Zend framework 2 form select element set required false - php

I have a select element as follows and I want to make it non required field. But I am unable to do so. It gives me error "Value is required and can't be empty"
$this->add(array(
'name' => 'civil_status',
'type' => 'Select',
'attributes' => array(
'id' => 'civil_status',
'class' => 'form-control',
),
'options' => array(
'empty_option' => 'Civil status',
'value_options' => array(
'married' => 'Married',
'single' => 'Single',
'other' => 'other',
),
)
));
I have set the validation as follows
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => FALSE,
'allow_empty' => TRUE,
'filters' => array(
array('name' => 'StripTags'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => '2',
'max' => '255',
),
),
),
)));
I tried to remove required from input filter as above and even more I have tried to remove required validation from the controller too as follows. But nothing seams to work. Please let me know if you guys know to fix this issue.
$form->getInputFilter()->get("civil_status")->setRequired(FALSE);
$form->getInputFilter()->get("civil_status")->setAllowEmpty(TRUE);

Select has build in InArray validator you dont need to use StringLength here or filters you only need to use 'required' => false which should work.
$this->inputFilter->add($factory->createInput(array(
'name' => 'civil_status',
'required' => false,
)));
I know it might be strange question but did you add your InputFilter to your Form before you hit $form->IsValid() ?

Related

Zend Form Element Select - how to return integer?

Is there any way to configure Zend\Form\Element\Select to return integer?
If I have a Form with a Select Element something like this (this is the common way to configure Select Element according to documentation and my internet research):
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
'1' => 'Gold',
'2' => 'Silver',
'3' => 'Diamond',
'4' => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
I thought If I change value option like this:
$this->add(array(
'name' => 'category_id',
'type' => 'Zend\Form\Element\Select',
'options' => array(
'label' => 'Category',
'value_options' => array(
1 => 'Gold',
2 => 'Silver',
3 => 'Diamond',
4 => 'Charm'
),
'attributes' => array(
'class' => 'form-control',
),
));
the integer will be returned but I was wrong. In both cases string is returned. My php code write this form values to a db table where category_id is defined as int.
In ZF2 use the Zend\Filter\Int or Zend\Filter\ToInt depending on which version of ZF2 you are using, Zend\Filter\Int became deprecated in ZF2.4.
In your form, assuming you are using the Zend\InputFilter\InputFilterProviderInterface use:
public function getInputFilterSpecification()
{
return array(
'category_id' => array(
'required' => TRUE,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
// Your validators here
),
),
);
}

zend 2 forms validatator groups

i am using the getInputFilterSpecification() to validate my form elements i.e
public function getInputFilterSpecification()
{
return array(
'comment' => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
),
'name' => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
),
),
),
),
}
my question is: rather than repeat the same validators for each separate element in my forms, is its possible to lump all the elements into an array and to get the valiators to loop all over them .
i.e somthing like this:
$validateThisGroup = $inputFilter->setValidationGroup(array('comment', 'name'));
return array(
$validateThisGroup => array(
'required' => true,
'filters' =>array(
array('name'=>'StripTags'),
array('name'=>'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
),
),
),
),
UPDATE;
I have reworded my question. I am basically trying to find out if there is a validator group function for the input filter specification function.
Not sure if my advice deserves to be an fully fledged answer but anyways :)
Validators need to be created separately for each input, they are separate stateful instances created by InputFilter's plugin manager, so you need to pass separate definition of validators for each input.
If you just want to avoid copypasting, define validator definition before returning and plug it into return array.
$inputValidatorsSpec = [
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 500,
],
],
];
$inputFiltersSpec = [
['name' => 'StripTags'],
['name' => 'StringTrim'],
];
return [
'comment' => [
'required' => true,
'filters' => $inputFiltersSpec,
'validators' => $inputValidatorsSpec
],
'name' => [
'required' => true,
'filters' => $inputFiltersSpec,
'validators' => $inputValidatorsSpec
]
];

ZF2: How do I use InArray validator to validate Multiselect form element?

I have a ZF2 form where I had to disable native validators, for a specific reason.
Then, when adding elements programatically to the form I also add validators.
One of the elements is a Multiselect array.
$form->add( array(
'type' => 'Zend\Form\Element\Select',
'options' => array(
(
'label' => 'few items',
'value_options' => Array
(
'one' => 'one',
'two' => 'two',
'three' => 'three',
'four' => 'four',
)
),
'attributes' => array
(
'multiple' => 'multiple',
'value' => array('two','three'),
'required' => 1,
'id' => 'few_items'
),
'name' => 'few_items'
));
Also, I'm going to add an InArray validator:
if($f instanceof \Zend\Form\Element\Select){
$inputFilter->add($factory->createInput(array(
'name' => $f->getName(),
'required' => $f->getAttribute('required') == 1,
'validators' => array(
array(
'name' => 'InArray',
'options' => array(
'haystack' => $f->getValueOptions(),
'messages' => array(
InArray::NOT_IN_ARRAY => 'Please select an option',
),
),
),
),
)));
}
The problem is that the validator always fails, because in POST multiselect field will return an array, and actually looking inside the InArray validator, it uses in_array(...) PHP function which is not suitable for this - array_intersect would do the trick, but before writing my own validator I do have a feeling that this wheel was invented already!
Having looked around I see that there was a bug raised to this effect (http://framework.zend.com/issues/browse/ZF2-413), and the solution was to introduce Explode validator, but I'm not sure how to add it into my input filter.
Thanks for your suggestions.
Actually, following the bugfix link, I figured out how to do the validation. Explode validator would break down the value and apply a validator to each part:
if($f instanceof \Zend\Form\Element\Select){
$inputFilter->add($factory->createInput(array(
'name' => $f->getName(),
'required' => $f->getAttribute('required') == 1,
'validators' => array(
array(
'name' => 'Explode',
'options' => array(
'validator' => new InArray(array(
'haystack' => $f->getValueOptions(),
'valueDelimeter' => null,
'messages' => array(
InArray::NOT_IN_ARRAY => 'Please select an option',
),
))
)
),
),
)));
}
Leaving this question here, because I haven't found any other answers to this myself and hopefully this will assist people in the future.

ZF2 Null Input Filter and Digits Validator

I have a model that implements InputFilterAwareInterface. For one of the fields I wish to validate that user input is digits and use null filter to ensure db field is set to null. Is there a way to do this? The following doesn't work.
$inputFilter->add(array(
'name' => '_programme_id',
'required' => false,
'allow_empty' => true,
'validators' => array(
array(
'name' => 'Digits',
)
),
'filters' => array(
array(
'name' => 'Null',
'options' => array(
'type' => 'all'
),
)
),
)
);
Validation fails with message "Invalid type given. String, integer or float expected"
Seems like this is because filtering happens before validation. Is there a quick way to achieve this behaviour?
You can convert your value to integer with the Int filter before passing it to Null filter, as follows:
$inputFilter->add(array(
'name' => '_programme_id',
'required' => false,
'allow_empty' => true,
'validators' => array(
array(
'name' => 'Digits',
)
),
'filters' => array(
array(
'name' => 'Int',
)
array(
'name' => 'Null',
'options' => array(
'type' => 'all'
),
)
),
)
);

Zend Framework 2 - Integer Form Validation

I've got the following problem. I wrote (based on the tutorial) a form validation. The text fields work just fine but the integer field behave odd.
This is my validator:
$inputFilter->add($factory->createInput(array(
'name' => 'zip',
'required' => false,
'filters' => array(
array('name' => 'Int'),
),
)));
It lies within my Entity.php like the other filters. The odd thing is that this one accepts not even a string but ignores the required when I set it to true. I tried to replace Int with Digits which then causes the form to accept required but still accepts strings.
Any ideas? Thanks!
Try using the Between validator:
$inputFilter->add($factory->createInput(array(
'name' => 'zip',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
'validators' => array(
array(
'name' => 'Between',
'options' => array(
'min' => 1,
'max' => 1000,
),
),
),
)));
this is a old topic but i should mention that Filters don't cause validation errors, they work in background and do their jobs silently .
for example Int filter will remove any non-integer from the input , so when you do $form->getData() the field with the Int filter will only have integer values and 0 if its empty.
array(
'name' => 'not_empty',
),
array(
'name' => 'Digits',
), array(
'name' => 'Between',
'options' => array(
'min' => 0,
'max' => 1,
),
),

Categories