maximkou
maximkou

Reputation: 5332

Nette Forms Validation - how to set field not required?

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.

Upvotes: 2

Views: 1612

Answers (1)

Josef Nevoral
Josef Nevoral

Reputation: 116

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');

Upvotes: 7

Related Questions