mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 07:45:45 +01:00
Since we have the ability to use `not` as a prefix, having rules that validate negative behaviour makes them a bit inflexible, verbose, and harder to understand. This commit will refactor the `NotEmpty`, and rename it to `Falsy`. It will no longer trim strings, because Blank does a much better job at it; it only simulates the behaviour of PHP’s native `empty()` function. Because `Falsy`, `Blank`, and `Undef` have similar behaviour, I created a page to demonstrate the difference and show when the user should use one or the other. Assisted-by: Cursor (claude-4.5-opus-high)
3.1 KiB
3.1 KiB
Attributes
Attributes()
Validates the PHP attributes defined in the properties of the input.
Example of object:
use Respect\Validation\Rules as Rule;
#[Rule\AnyOf(
new Rule\Property('email', new Rule\Not(new Rule\Undef())),
new Rule\Property('phone', new Rule\Not(new Rule\Undef())),
)]
final class Person
{
public function __construct(
#[Rule\Not(new Rule\Undef())]
public string $name,
#[Rule\Date('Y-m-d')]
#[Rule\DateTimeDiff('years', new Rule\LessThanOrEqual(25))]
public string $birthdate,
#[Rule\Email]
public ?string $email = null,
#[Rule\Phone]
public ?string $phone = null,
) {
}
}
Here is how you can validate the attributes of the object:
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com'));
// No exception
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com', '+12024561111'));
// No exception
v::attributes()->assert(new Person('', '2020-06-23', 'john.doe@gmail.com', '+12024561111'));
// Message: `.name` must not be empty
v::attributes()->assert(new Person('John Doe', 'not a date', 'john.doe@gmail.com', '+12024561111'));
// Message: `.birthdate` must be a valid date in the format "2005-12-30"
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'not an email', '+12024561111'));
// Message: `.email` must be a valid email address or must be null
v::attributes()->assert(new Person('John Doe', '2020-06-23', 'john.doe@gmail.com', 'not a phone number'));
// Message: `.phone` must be a valid telephone number or must be null
v::attributes()->assert(new Person('John Doe', '2020-06-23'));
// Full message:
// - `Person { +$name="John Doe" +$birthdate="2020-06-23" +$email=null +$phone=null +$address=null }` must pass at least one of the rules
// - `.email` must be defined
// - `.phone` must be defined
v::attributes()->assert(new Person('', 'not a date', 'not an email', 'not a phone number'));
// Full message:
// - `Person { +$name="" +$birthdate="not a date" +$email="not an email" +$phone="not a phone number" +$address=null }` must pass the rules
// - `.name` must not be empty
// - `.birthdate` must pass all the rules
// - `.birthdate` must be a valid date in the format "2005-12-30"
// - For comparison with now, `.birthdate` must be a valid datetime
// - `.email` must be a valid email address or must be null
// - `.phone` must be a valid telephone number or must be null
Caveats
- If the object has no attributes, the validation will always pass.
- When the property is nullable, this rule will wrap the rule on the property into NullOr rule.
- This rule has no templates because it uses the templates of the rules that are applied to the properties.
Categorization
- Objects
- Structures
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Created |
See also: