Class ValidationSet
ValidationSet object. Holds all validation rules for a field and exposes methods to dynamically add or remove validation rules
Property Summary
-
$_allowEmpty protected
bool|string|callable
Denotes if a field is allowed to be empty
-
$_rules protected
Cake\Validation\ValidationRule[]
Holds the ValidationRule objects
-
$_validatePresent protected
bool|string|callable
Denotes whether the fieldname key must be present in data array
Method Summary
-
add() public
Sets a ValidationRule $rule with a $name
-
allowEmpty() public
Sets whether a field value is allowed to be empty.
-
count() public
Returns the number of rules in this set
-
getIterator() public
Returns an iterator for each of the rules to be applied
-
isEmptyAllowed() public
Sets whether a field value is allowed to be empty.
-
isPresenceRequired() public
Sets whether a field is required to be present in data array.
-
offsetExists() public
Returns whether an index exists in the rule set
-
offsetGet() public
Returns a rule object by its index
-
offsetSet() public
Sets or replace a validation rule
-
offsetUnset() public
Unsets a validation rule
-
remove() public
Removes a validation rule from the set
-
requirePresence() public
Sets whether a field is required to be present in data array.
-
rule() public
Gets a rule for a given name if exists
-
rules() public
Returns all rules for this validation set
Method Detail
add() ¶ public
add(string $name, Cake\Validation\ValidationRule|array $rule): $this
Sets a ValidationRule $rule with a $name
Example:
$set
->add('notBlank', ['rule' => 'notBlank'])
->add('inRange', ['rule' => ['between', 4, 10])
Parameters
-
string
$name The name under which the rule should be set
-
Cake\Validation\ValidationRule|array
$rule The validation rule to be set
Returns
$this
allowEmpty() ¶ public
allowEmpty(bool|string|callable $allowEmpty): $this
Sets whether a field value is allowed to be empty.
Parameters
-
bool|string|callable
$allowEmpty Valid values are true, false, 'create', 'update' or a callable.
Returns
$this
getIterator() ¶ public
getIterator(): ArrayIterator
Returns an iterator for each of the rules to be applied
Returns
ArrayIterator
isEmptyAllowed() ¶ public
isEmptyAllowed(bool|string|callable|null $allowEmpty = null): bool|string|callable
Sets whether a field value is allowed to be empty.
If no argument is passed the currently set allowEmpty
value will be returned.
Parameters
-
bool|string|callable|null
$allowEmpty optional Deprecated since 3.6.0 ValidationSet::isEmptyAllowed() is deprecated as a setter. Use ValidationSet::allowEmpty() instead.
Returns
bool|string|callable
isPresenceRequired() ¶ public
isPresenceRequired(bool|string|callable|null $validatePresent = null): bool|string|callable
Sets whether a field is required to be present in data array.
If no argument is passed the currently set validatePresent
value will be returned.
Parameters
-
bool|string|callable|null
$validatePresent optional Deprecated since 3.6.0 ValidationSet::isPresenceRequired() is deprecated as a setter Use ValidationSet::requirePresence() instead.
Returns
bool|string|callable
offsetExists() ¶ public
offsetExists(string $index): bool
Returns whether an index exists in the rule set
Parameters
-
string
$index name of the rule
Returns
bool
offsetGet() ¶ public
offsetGet(string $index): Cake\Validation\ValidationRule
Returns a rule object by its index
Parameters
-
string
$index name of the rule
Returns
Cake\Validation\ValidationRule
offsetSet() ¶ public
offsetSet(string $index, Cake\Validation\ValidationRule|array $rule): void
Sets or replace a validation rule
Parameters
-
string
$index name of the rule
-
Cake\Validation\ValidationRule|array
$rule Rule to add to $index
Returns
void
offsetUnset() ¶ public
offsetUnset(string $index): void
Unsets a validation rule
Parameters
-
string
$index name of the rule
Returns
void
remove() ¶ public
remove(string $name): $this
Removes a validation rule from the set
Example:
$set
->remove('notBlank')
->remove('inRange')
Parameters
-
string
$name The name under which the rule should be unset
Returns
$this
requirePresence() ¶ public
requirePresence(bool|string|callable $validatePresent): $this
Sets whether a field is required to be present in data array.
Parameters
-
bool|string|callable
$validatePresent Valid values are true, false, 'create', 'update' or a callable.
Returns
$this
rule() ¶ public
rule(string $name): Cake\Validation\ValidationRule|null
Gets a rule for a given name if exists
Parameters
-
string
$name The name under which the rule is set.
Returns
Cake\Validation\ValidationRule|null
rules() ¶ public
rules(): Cake\Validation\ValidationRule[]
Returns all rules for this validation set
Returns
Cake\Validation\ValidationRule[]
Property Detail
$_validatePresent ¶ protected
Denotes whether the fieldname key must be present in data array
Type
bool|string|callable