Hey,
Just a little bit of context:
I’m using OptionalInputFilter into my forms because I have multiple inputs depending of others. For example: if you have to complete a payment form between Bank payment or Credit card payment and all the inputs you can imagine.
Before OptionalInputFilter, I read in blogs that people recommend to set your ValidationGroup array for each case, but when I was refactoring my code I found its not a good idea set this in Controller and I prefer to do all the stuff of Form inside itself (at least for me ).
For that reason I began to use OptionalInputFilter.
Here my form class:
<?php
namespace Application\Form;
use Laminas\Form\Form;
use Laminas\InputFilter\OptionalInputFilter;
class BillingForm extends Form
{
public function __construct()
{
parent::__construct('profileForm');
$this->addElements();
$this->addInputFilter();
}
protected function addElements()
{
$address = new Fieldset\Address($this->em);
$this->add($address->get('address'));
$this->add($address->get('address_height'));
$this->add([
'name' => 'submit',
'type' => 'submit',
'attributes' => [
'class' => 'btn btn-sm btn-primary',
'value' => 'Guardar cambios',
]
]);
}
protected function addInputFilter()
{
$inputFilter = $this->getInputFilter();
$addressFilter = new OptionalInputFilter();
$addressFilter->add([
'name' => 'address',
'required' => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 2,
'max' => 50
],
],
],
]);
$addressFilter->add([
'name' => 'address_height',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'Digits'],
[
'name' => 'StringLength',
'options' => [
'max' => 10
],
],
],
]);
$inputFilter->add($addressFilter, 'address_inputs');
}
}
And my Fieldset:
$this->add([
'name' => 'address',
'attributes' => [
'required' => true,
'class' => 'form-control required',
'maxlength' => 50,
'placeholder' => 'Calle'
],
'options' => [
'label' => 'Calle<sup class="red"><small>*</small></sup>',
'label_options' => [
'disable_html_escape' => true,
],
],
]);
$this->add([
'name' => 'address_height',
'type' => 'number',
'attributes' => [
'required' => true,
'class' => 'form-control required',
'maxlength' => 10,
'placeholder' => 'Altura'
],
'options' => [
'label' => 'Altura<sup class="red"><small>*</small></sup>',
'label_options' => [
'disable_html_escape' => true,
],
],
]);
When I submit the form, (using $form->setData($data) and check $form->getMessages() after $form->isValid() clause), I have this array:
array(1) { ["address_height"]=> array(1) { ["isEmpty"]=> string(50) "A value is required and it can not be empty" }
But … if I change type attribute from number to text, this input is valid.
What can it be?
This occurs with OptionalInputFilter and InputFilter, I just named it because there is not enough information about how to use it and I like to record this issue with this usage for other people that may google-it and find there are people using this .
Thanks.
Julian