respect-validation/docs/rules/UndefOr.md
Henrique Moody 707dcae65f
Refactor the "UndefOr" rule and related classes
This commit will rename the "Optional" rule to"UndefOr" while soft
deprecating the old name. It should work the same as the previous one
but with a different name. It will also prefix the result ID, allowing
more message customization.

While working on it, I realized that the prefix "undefOr" had a typo,
and it was using "undefOf" instead. I fixed that, too.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2024-03-26 01:35:36 +01:00

1.1 KiB

UndefOr

  • UndefOr(Validatable $rule)

Validates if the given input is undefined or not.

By undefined we consider null or an empty string (''), which implies that the input is not set. This is particularly useful when validating form fields

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

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

Note

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

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

Categorization

  • Nesting

Changelog

Version Description
3.0.0 Renamed from "Optional" to "UndefOr"
1.0.0 Created

See also: