respect-validation/docs/rules/Attribute.md

48 lines
853 B
Markdown
Raw Permalink Normal View History

2015-01-30 09:40:06 +01:00
# Attribute
- `Attribute(string $name)`
- `Attribute(string $name, Validatable $rule)`
- `Attribute(string $name, Validatable $rule, bool $mandatory)`
2015-01-30 09:40:06 +01:00
Validates an object attribute, even private ones.
2015-01-30 09:40:06 +01:00
```php
$obj = new stdClass;
$obj->foo = 'bar';
v::attribute('foo')->validate($obj); // true
2015-01-30 09:40:06 +01:00
```
You can also validate the attribute itself:
```php
v::attribute('foo', v::equals('bar'))->validate($obj); // true
2015-01-30 09:40:06 +01:00
```
Third parameter makes the attribute presence optional:
```php
2015-10-07 16:52:03 +02:00
v::attribute('lorem', v::stringType(), false)->validate($obj); // true
2015-01-30 09:40:06 +01:00
```
The name of this validator is automatically set to the attribute name.
## Categorization
- Nesting
- Objects
- Structures
## Changelog
Version | Description
--------|-------------
0.3.9 | Created
***
2015-01-30 09:40:06 +01:00
See also:
- [Key](Key.md)
2018-12-11 13:31:50 +01:00
- [KeyNested](KeyNested.md)
- [ObjectType](ObjectType.md)