Reputation: 247
I wanted to bypass the annoying validation for the Select Element in Zend Framework 2. My requirement is that I have two select drop down boxes where I pass the values from the left select box to right select box and finally when I click the submit button, I post the values from right select box.
So basically the left select box is there to show the values to the user, my problem is that in the left select box as no value is selected it throws this error:
["selectAll"] => array(1) {
["isEmpty"] => string(36) "Value is required and can't be empty"
}
I have seen some old posts from Stackoverflow, where this is possible in Zend Framework 1 like one can disable the validation for the particular select element´
Examples:-
$selectAllElement->setRequired(false);
or
$selectAllElement->removeDecorator('Errors');
which some how these methods are not available in Zend Framework 2. In case if somebody has a solution of how disable the validation for a particular select element, please do share your knowledge, will be hepful for people getting into Zend Framework 2.
EDIT:
In my Form-
$select = new Element\Select('selectAllElement');
$select->setAttribute('title', 'Select a Value')
->setAttribute('id', 'id');
$options = array(
array(
//Fetching the values from database
),
);
$select->setAttribute('multiple', 'multiple')
->setAttribute('required', false)
->setValueOptions($options);
$this->add($select);
As per request from Sam, I have provided the code how I am adding the select element to my form and setting the attributes.
NOTE: I am not using any FIELDSETS
Upvotes: 2
Views: 7301
Reputation: 1
I know this topic is most probably very outdated, but still maybe one consider this as useful.
In version of zf2 which I'm using there is a bug in class Zend\Form\Element\Select, this class provides InputProviderInterface, especially method getInputSpecification(), in this method option
$spec = array(
'name' => $this->getName(),
'required' => true,
);
So if you redefine method getInputSpecification it should works.
Upvotes: 0
Reputation: 143
Sometimes with me, the setRequired(false) not works when working with select multiple or checkbox that is not in the view, and so, i just override the isValid method and remove the validator as the code above:
public function isValid() {
$this->getInputFilter()->remove('alterarsenha');
$this->getInputFilter()->remove('usuario_perfis');
return parent::isValid();
}
Upvotes: 1
Reputation: 16455
To clarify this as an answer, using the setRequired()
-Method on any Zend\Form\Element only declares if the HTML-Attribute required="(bool)"
should be rendered or not.
If you want to exclude a Field/Element from Validation you need to define this inside your InputFilters setting 'required' => false
Upvotes: 6