mirror of
https://github.com/Respect/Validation.git
synced 2024-06-13 19:22:16 +02:00
24885e4a5f
Because we have the Key and Property rules, the KeyNested is redundant, although it's a helpful shortcut. The real problem is dealing with messages and templates because the structure of the validator needs to match the structure of the rule. When using the `getMessages()` method from the exception we throw in `assert()`, we get a flat structure, which is often not the intended structure. The KeyNested rule is cool, but it adds some complexity to the codebase that I'm unwilling to deal with. It's not nice to remove a rule, especially because I know people use it, but it's for the best. I'm trying to keep the codebase small, so hopefully, it will get easier to maintain. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2.1 KiB
2.1 KiB
Property
Property(string $propertyName, Validatable $rule)
Validates an object property against a given rule.
$object = new stdClass;
$object->name = 'The Respect Panda';
$object->email = 'therespectpanda@gmail.com';
v::property('name', v::equals('The Respect Panda'))->validate($object); // true
v::property('email', v::email())->validate($object); // true
v::property('email', v::email()->endsWith('@example.com'))->assert($object); // false
You can also use Property
to validate nested objects:
$object->address = new stdClass();
$object->address->postalCode = '1017 BS';
v::property(
'address',
v::property('postalCode', v::postalCode('NL'))
)->validate($object); // true
The name of this validator is automatically set to the property name.
v::property('website', v::url())->assert($object);
// message: website must be present
v::property('name', v::uppercase())->assert($object);
// message: name must be uppercase
Note
This rule will validate public, private, protected, uninitialised, and static properties.
- To only validate if a property exists, use PropertyExists instead.
- To validate a property against a given rule only if the property exists, use PropertyOptional instead.
Categorization
- Nesting
- Objects
- Structures
Changelog
Version | Description |
---|---|
3.0.0 | Renamed from Attribute to Property , and split by PropertyExists and PropertyOptional |
0.3.9 | Created |
See also: