How to use OptionalInputFilter and ways to validate it

16 Views Asked by At

I was looking for how to use OptionalInputFilter and the only page we can found leaves me some questions about its behavior.

I have a form with 2 inputs:

  1. billing_type_id
  2. tax_number.

The first one is always required but there is an option that doesn't require a tax_number filled, so I handle it by Javascript, showing and hiding tax_number input.

The second one must be required and validated if its value is not empty. It must be integer.

So I wrote this:

$this->add([
   'name' => 'tax_name',
   'attributes' => [
       'required' => true
   ]
]);

$taxNumber = new OptionalInputFilter();
$taxNumber->add([
   'name' => 'tax_number',
   'required' => true,
   'validators' => ['name' => 'IsInt'],
]);

$this->getInputFilter()->add($taxNumber, 'tax_filter');

I understand that if tax_number has value, input filter will run, but I enter “text” on it, my form is still valid. Is this input filter running? Do I need to add tax_filter to validationGroup?

So what does

optional inputs [...] are required if present

mean in docs?

I hope I was clear.

Thanks.

0

There are 0 best solutions below