mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 23:05:45 +01:00
- Remove redundant "valid" prefix: Date, DateTime, DateTimeDiff, Domain, Email, Iban, Imei, Ip, Isbn, Json, LanguageCode, LeapDate, LeapYear, Luhn, MacAddress, NfeAccessKey, Nif, Nip, Pesel, Phone, Pis, PolishIdCard, PostalCode, Roman, Slug, Tld, Url, Uuid, Version. - Remove redundant "value" suffix ArrayVal, BoolVal, Countable, FloatVal, IntVal, IterableVal, NumericVal, ScalarVal, StringVal. - Standardize "consist only of" phrasing Alnum, Alpha, Cntrl, Consonant, Digit, Graph, Lowercase, Printable, Punct, Space, Spaced, Uppercase, Vowel, Xdigit. - Improve file accessibility messages Directory, Executable, File, Image, Readable, SymbolicLink, Writable. - Improve grammar and article usage CreditCard, Extension, Mimetype, Regex, Size.
85 lines
2.1 KiB
Markdown
85 lines
2.1 KiB
Markdown
<!--
|
|
SPDX-License-Identifier: MIT
|
|
SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
-->
|
|
|
|
# NullOr
|
|
|
|
- `NullOr(Validator $validator)`
|
|
|
|
Validates the input using a defined validator when the input is not `null`.
|
|
|
|
## Usage
|
|
|
|
```php
|
|
v::nullOr(v::email())->assert(null);
|
|
// Validation passes successfully
|
|
|
|
v::nullOr(v::email())->assert('example@example.com');
|
|
// Validation passes successfully
|
|
|
|
v::nullOr(v::email())->assert('not an email');
|
|
// → "not an email" must be an email address or must be null
|
|
```
|
|
|
|
## Prefix
|
|
|
|
For convenience, you can use `nullOr` as a prefix to any validator:
|
|
|
|
```php
|
|
v::nullOrEmail()->assert('not an email');
|
|
// → "not an email" must be an email address or must be null
|
|
|
|
v::nullOrBetween(1, 3)->assert(2);
|
|
// Validation passes successfully
|
|
|
|
v::nullOrBetween(1, 3)->assert(null);
|
|
// Validation passes successfully
|
|
```
|
|
|
|
## Templates
|
|
|
|
### `NullOr::TEMPLATE_STANDARD`
|
|
|
|
| Mode | Template |
|
|
| ---------: | :------------------- |
|
|
| `default` | or must be null |
|
|
| `inverted` | and must not be null |
|
|
|
|
## Template as suffix
|
|
|
|
The template serves as a suffix to the template of the inner validator.
|
|
|
|
```php
|
|
v::nullOr(v::alpha())->assert('has1number');
|
|
// → "has1number" must consist only of letters (a-z) or must be null
|
|
|
|
v::not(v::nullOr(v::alpha()))->assert("alpha");
|
|
// → "alpha" must not consist only of letters (a-z) and must not be null
|
|
```
|
|
|
|
## Template placeholders
|
|
|
|
| Placeholder | Description |
|
|
| ----------- | ---------------------------------------------------------------- |
|
|
| `subject` | The validated input or the custom validator name (if specified). |
|
|
|
|
## Categorization
|
|
|
|
- Nesting
|
|
|
|
## Changelog
|
|
|
|
| Version | Description |
|
|
| ------: | :-------------------- |
|
|
| 3.0.0 | Templates changed |
|
|
| 3.0.0 | Renamed to `NullOr` |
|
|
| 2.0.0 | Created as `Nullable` |
|
|
|
|
## See Also
|
|
|
|
- [Attributes](Attributes.md)
|
|
- [NullType](NullType.md)
|
|
- [UndefOr](UndefOr.md)
|