mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 23:35:45 +01:00
This commit introduces REUSE compliance by annotating all files with SPDX information and placing the reused licences in the LICENSES folder. We additionally removed the docheader tool which is made obsolete by this change. The main LICENSE and copyright text of the project is now not under my personal name anymore, and it belongs to "The Respect Project Contributors" instead. This change restores author names to several files, giving the appropriate attribution for contributions.
2.1 KiB
2.1 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())->assert('');
// Validation passes successfully
v::undefOr(v::digit())->assert(null);
// Validation passes successfully
v::undefOr(v::alpha())->assert('username');
// Validation passes successfully
v::undefOr(v::alpha())->assert('has1number');
// → "has1number" must contain only letters (a-z) or must be undefined
Prefix
For convenience, you can use the undefOr as a prefix to any validator:
v::undefOrEmail()->assert('not an email');
// → "not an email" must be a valid email address or must be undefined
v::undefOrBetween(1, 3)->assert(2);
// Validation passes successfully
Templates
UndefOr::TEMPLATE_STANDARD
| Mode | Template |
|---|---|
default |
or must be undefined |
inverted |
and must not be undefined |
Template as suffix
The template serves as a suffix to the template of the inner validator.
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: