Options - rlanvin/php-form GitHub Wiki

allow_empty

  • Default: true

Non-required empty values are considered valid by default. If they weren't, and if they were passed to the other rules, you would not be able to validate situations like "The URL is optional, but if provided it must be a valid URL" (empty string would not be considered a valid URL), or "The name is optional, but if provided it must be minimum 6 characters" (empty string would be zero characters).

You can change this behavior, and force rules to be applied even to empty values with allow_empty option set to false. Example:

$form = new Form\Validator([
    'name' => ['min_length' => 2]
]);
$form->validate(array('name' => '')); // this is true, the name is empty and not required, so we ignore other validators
$form->validate(array('name' => '', ['allow_empty' => false]); // now this is false, and you'll get a min_length error

stop_on_error

  • Default: true

Stop the validation on the first failure, instead of going through all the checks.

use_default

  • Default: true

If you want the Form to ignore default value, and treat missing keys as empty values, you can call validate() with the option use_default set to false. Example:

$form = new Form\Validator([
    'name' => ['required', 'min_length' => 2]
]);
$form->setValues(array('name' => 'Default name'));

$form->validate(array(), ['use_default' => false]); // false, name is required and the form ignores default value

ignore_extraneous

  • Default: true

Ignore extraneous input values, i.e. values that have no rule attached to them. If you want the validation to fail when unrecognized values are present in the input values array, set this option to false.