respect-validation/docs/rules/NullOr.md
Henrique Moody 7cec227520
Create "Attribute" rule
With this change, any rule can be used as a PHP attribute. I have wanted
to implement this feature for a while, as it allows you to bind the
validation to a specific property and just validate the object
afterwards.
2024-12-13 03:49:29 +01:00

1.5 KiB

NullOr

  • NullOr(Rule $rule)

Validates the input using a defined rule when the input is not null.

Usage

v::nullable(v::email())->isValid(null); // true
v::nullable(v::email())->isValid('example@example.com'); // true
v::nullable(v::email())->isValid('not an email'); // false

Prefix

For convenience, you can use nullOr as a prefix to any rule:

v::nullOrEmail()->isValid('not an email'); // false
v::nullOrBetween(1, 3)->isValid(2); // true
v::nullOrBetween(1, 3)->isValid(null); // true

Templates

NullOr::TEMPLATE_STANDARD

Mode Template
default or must be null
inverted and must not be null

The templates from this rule serve as message suffixes:

v::nullOr(v::alpha())->assert('has1number');
// "has1number" must contain only letters (a-z) or must be null

v::not(v::nullOr(v::alpha()))->assert("alpha");
// "alpha" must not contain letters (a-z) and must not be null

Template placeholders

Placeholder Description
name The validated input or the custom validator name (if specified).

Categorization

  • Nesting

Changelog

Version Description
3.0.0 Renamed to NullOr
2.0.0 Created as Nullable

See also: