Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Using presenceOf validator on a File element

Hi

I'm making a Form in order to upload a file. I added a validator in order to check if the File filed is empty. But i already check this in my controller and furthermore the validation returns always false although without the validation, the field works perfectly fine. So is there a way/reason to us presenceOf validator on a File Element in a form ?

Greetings

Brieuc

what is your code in your controller and in your form

Hi this is a part of my PictureForm.php

$picture = new File("picture", array(
    "class" => "form-control"
));
/*$picture->addValidator(new PresenceOf(array(
    "message" => "'File' field can't be empty !"
)));*/
$this->add($picture);

and there is the part of my PictureController.php

    public function addAction()
    {
        $form = new PictureForm();
        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                if($this->request->hasFiles())
                {
                    foreach($this->request->getUploadedFiles() as $file)
                    {
                        $picture = new Picture();
                        $picture->assign(array(
                            "src" => "img/photos/".$file->getName(),
                            "alt" => $file->getName(),
                        ));
                        if($picture->save() and $file->moveTo("img/photos/".$file->getName()))
                        {
                            $this->flashSession->success("Great ! You're picture was successfully uploaded !");
                            $response = new Response();
                            return $response->redirect(array(
                                "for" => "adminIndex",
                                "controller" => "admin"
                            ));
                        }
                    }
                }
                else
                {
                    $this->flash->error("'File' field can't be empty !");
                }
            }
            else
            {
                foreach($form->getMessages() as $message)
                {
                    $this->flash->error($message);
                }
            }
        }
        $this->view->setVar("form", $form);
        $this->view->pick("admin/picture/add");
    }

As you can see i've commented the validator and i'm validating the file field manually in the controller . I'm wondering if the solution with the validator which ain't working in my case is a better solution or not ?



2.4k
Accepted
answer

just try this code

$picture->addValidators(array( new PresenceOf(array( 'message'=>'File is required' ))

));

I really understand why but it works you're way and it's not with the addValidator ... This behaviour is odd ... I'm really dazed on this one .. But thanks a lot !

its my pleasure just keep it up :) thanks to