I would like to validate the file extension only if a file is uploaded.
I have a collection of fieldsets that include a File Input element. If I want to upload a file for only one of the fieldsets and leave the rest of the File Input elements empty the form is not validated, although they have required = false. This validation triggers the "fileExtensionNotFound" error.
Is there a way to add the AllowEmpty to or before the Extension validator?
'file' => array(
'required' => false,
'validators' => array(
array(
'name' => 'Zend\Validator\File\Extension',
'options' => array(
'extension' => array('pdf', 'xls','doc'),
)
)
)
)
I had to specify the type to be FileInput.
'file' => array(
'type' => '\Zend\InputFilter\FileInput',
'allow_empty' => true,
'required' => false,
'validators' => array(
array(
'name' => 'Zend\Validator\File\Extension',
'options' => array(
'extension' => array('pdf', 'xls','doc')
),
),
),
),
Related
I want to create the following element :
<input type="file" name="file[]">
I have tried the following code in myproject/module/Member/src/Member/Form/EditForm.php :
$this->add(array(
'name' => 'file',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
and
$this->add(array(
'name' => 'file[]',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
but it is not working.
For file upload Zend Framework 2 has a special FileInput class.
It is important to use this class because it also does other important things like validation before filtering. There are also special filters like the File\RenameUpload that renames the upload for you.
Considering that $this is your InputFilter instance the code could look like this:
$this->add(array(
'name' => 'file',
'required' => true,
'allow_empty' => false,
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => 'upload',
'randomize' => true,
'overwrite' => true
)
)
),
'validators' => array(
array(
'name' => 'FileSize',
'options' => array(
'max' => 10 * 1024 * 1024 // 10MB
)
)
),
// IMPORTANT: this will make sure you get the `FileInput` class
'type' => 'Zend\InputFilter\FileInput'
);
To attach file element to a form:
// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
->setAttribute('id', 'file');
$this->add($file);
Check the documentation for more information on file upload.
Or check the documentation here on how to make an upload form
How I set the inputFilter to not allow white space in zend framework 2?
I'm trying this:
$inputFilter->add($factory->createInput(array(
'name' => 'codigo',
'required' => true,
'validators' => array(
array(
'name' => 'not_empty',
),
),
'filters' => array(
array(
'name' => 'Alnum',
'allowwhitespace' => false,
),
),
)));
Few points in your code needs minor tweaks;
ValidatorPluginManager uses normalized aliases to invoke
validators by cannonical names, which means 'not_empty' is not a
valid alias, it should be 'notempty' or 'NotEmpty'.
Also your Alnum filter signature seems invalid. You should
provide additional options inside the options sub key with underscores. (Yes, this is really weird inconsistency)
Try this:
$filter = new \Zend\InputFilter\InputFilter();
$filter->add(array(
'name' => 'codigo',
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
),
),
'filters' => array(
array(
'name' => 'Alnum',
'options' => array(
'allow_white_space' => false,
)
),
),
));
$filter->setData(['codigo' => 'Whitespace exists']);
if($filter->isValid() === false) {
// You'll fall here with a value like multiple spaces etc..
var_dump($filter->getMessages());
} else {
var_dump($filter->getValues()); // Prints ['codigo' => string 'Whitespaceexists']
}
I just created a form in cakephp and I would like to do a validation for one of the field.
The form will only be submitted if this particular field is left empty.
Here's a my particular field
<?php echo
$this->Form->input('MyForm.fieldA', array(
'type' => 'text',
'label' => '',
'class' => 'span3 detector-form',
))
?>
And my validation code:
public $validate = array(
'fieldA' => array(
'rule' => 'blank',
'on' => 'submit',
'message' => 'Failed authorize',
'last' => true
),
);
P/s. I tried using
public $validate = array(
'fieldA' => array(
'rule' => 'blank',
'on' => 'create',
'message' => 'Failed authorize',
'last' => true
),
);
But the 'create' sounds like only worked when the field is created.So I changed to 'submit' for a testing purpose.
I tried to use rule''=> 'Empty' as well but the validation doesn't work. Or is there any other alternative rules that I can use to reach this goal?
I am trying to validate a multiply select using input filter, but every time I see a error. The error is "notInArray":"The input was not found in the haystack".(I use ajax but it doesn`t metter).
I will show part of my code to be more clear.
in Controller:
if ($request->isPost()) {
$post = $request->getPost();
$form = new \Settings\Form\AddUserForm($roles);//
$form->get('positions')
->setOptions(
array('value_options'=> $post['positions']));
//.... more code...
When I put print_r($post['positions']); I see:
array(0 => 118, 1 => 119)
in ..../form/UserForm.php I create the multiply element
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
and in the validation file the code is:
$inputFilter->add($factory->createInput(array(
'name' => 'positions',
'required' => false,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => array(118,119),
'messages' => array(
'notInArray' => 'Please select your position !'
),
),
),
),
What can be the reason every time to see this error, and how I can fix it?
By default selects have attached InArray validator in Zend Framework 2.
If you are adding new one - you will have two.
You should disable default one as follow:
$this->add(array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
'disable_inarray_validator' => true, // <-- disable
),
'attributes' => array(
'multiple' => 'multiple',
'id' => 'choosed_positions',
),
'required' => false,
'name' => 'positions',
));
And you should get rid of the additional error message.
Please let us know if that would helped you.
I'm having problems implementing the IsImage File validator in a Form class in Zend Framework 2-beta5.
In general I'm having problems using any File validator in a Zend Form class in Zend framework 2.
I couldn't find any relevant documentation.
I found that for example Float validator that resides at Library/Zend/Validator
can be used with the following code:
$this->setInputFilter($inputFactory->createInputFilter(array(
'alcohol_vol' => array(
'name' => 'alcohol_vol',
'required' => true,
'validators' => array(
array(
'name' => 'Float',
),
),
),
)));
the IsImage file validator resides at /Library/Zend/Validator/File and can't find a way to use it. any information regarding the issue would be greatly appreciated.
thanks!
Kfir
Try to add this snippet under validators key, like this:
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => array(
'break_chain_on_failure' => true,
),
),
),
But sometimes, depends on server configuration, IsImage might not working. Then use Extension validator instead:
'validators' => array(
array(
'name' => '\Zend\Validator\File\Extension',
'options' => array(
'extension' => array(
'png', 'jpeg', 'jpg',
),
'break_chain_on_failure' => true,
),
),
),
Upload file validate/filter should use Zend\File\Transfer but not Zend\Form
Try below way to add file validator
$fileTransfer = new Zend\File\Transfer\Transfer();
$fileTransfer->addValidators(array(
array('IsImage', true)
));
if($fileTransfer->isValid()){
$fileTransfer->receive();
}