2

I add text inputs into a form and add them some rules. These inputs are not required, but if I do not fill these fields, I get a validation error.

Example:

$this->addText('operationID', 'Operation ID:')
    ->setAttribute('class', 'tf tf-w110px')
    ->addRule(Nutnet_Form::INTEGER, 'Operation ID must be integer');

How to properly set rule, that would accept not filled operationID field, instead of giving Operation ID must be integer error?

Using conditions helps, but it is not comfortable.

Vojtech Kane
  • 559
  • 6
  • 21
maximkou
  • 5,252
  • 1
  • 20
  • 41
  • 1
    default INTEGER validation requires valid number. NULL, empty string, etc. is not a valid integer. – hrach Jun 19 '14 at 23:04

1 Answers1

7

Using condition is the correct way. Why do you think it's not comfortable?

$this->addText('operationID', 'Operation ID:')
    ->setAttribute('class', 'tf tf-w110px')
    ->addCondition(Nutnet_Form::FILLED)
        ->addRule(Nutnet_Form::INTEGER, 'Operation ID must be integer');