respect-validation/docs/validators/UndefOr.md
Henrique Moody 81310cc4d9
Rename namespace Rules to Validators
Since that namespace contains our “validators”, naming it as such makes
much more sense.
2026-01-05 17:36:35 +01:00

1.7 KiB

UndefOr

  • UndefOr(Validator $validator)

Validates the input using a defined validator when the input is not null or an empty string ('').

This validator can be particularly useful when validating form fields.

Usage

v::undefOr(v::alpha())->isValid(''); // true
v::undefOr(v::digit())->isValid(null); // true

v::undefOr(v::alpha())->isValid('username'); // true
v::undefOr(v::alpha())->isValid('has1number'); // false

Prefix

For convenience, you can use the undefOr as a prefix to any validator:

v::undefOrEmail()->isValid('not an email'); // false
v::undefOrBetween(1, 3)->isValid(2); // true

Templates

UndefOr::TEMPLATE_STANDARD

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

The templates from this validator serve as message suffixes:

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

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

Template placeholders

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

Categorization

  • Nesting

Changelog

Version Description
3.0.0 Renamed to UndefOr
1.0.0 Created as Optional

See also: