I want to use an filter which will check if an photo was uploaded. The actual upload will not happen in the form, but will be excuted by javascript. Because the filename will differ for every user I tried to add an filter rule which checks the existence of the uploaded file. When the file does not exists I will show an error in the view with $this->formElementErrors($this->form->get('photo')).
I've tested the validator outside the filter (just in the controller) and that will work without problems. The solution should look like the code below.
$filter = new \Application\Form\SomeFilter(); // Existing filter rules.
$filter->add(array( // Add rule. There is no photo record available in SomeFilter()
'name' => 'photo',
'required' => true,
'validators' => array(
array(
'name' => 'Zend\Validator\File\Exists',
'options' => array(
'/directory/[different_number].jpeg',
)
)
)
));
$form->setInputFilter($filter);
I searched the internet for somebody who solved the same problem, but I could not find any solution. I guess there is something wrong with the options array, but I don't know what. Who knows how to solve this problem?
First thing is you're creating the Exists validator with wrong options signature. It should have a directory key and value need to be a directory, not full path + filename of the image.
Example:
Second detail is you need to set a value to your 'photo' input before calling the
isValid()method of your form/filter sinceExistsvalidator only knows the lookup directory, not the file's name.You can try something like this:
Hope it helps.