Fr3nch13/CakePHP Utilities

ValidationSet
in package
implements ArrayAccess, IteratorAggregate, Countable

ValidationSet object. Holds all validation rules for a field and exposes methods to dynamically add or remove validation rules

Table of Contents

Interfaces

ArrayAccess
IteratorAggregate
Countable

Properties

$_allowEmpty  : callable|string|bool
Denotes if a field is allowed to be empty
$_rules  : array<string|int, ValidationRule>
Holds the ValidationRule objects
$_validatePresent  : callable|string|bool
Denotes whether the fieldname key must be present in data array

Methods

add()  : $this
Sets a ValidationRule $rule with a $name
allowEmpty()  : $this
Sets whether a field value is allowed to be empty.
count()  : int
Returns the number of rules in this set
getIterator()  : Traversable<string, ValidationRule>
Returns an iterator for each of the rules to be applied
isEmptyAllowed()  : callable|string|bool
Returns whether a field can be left empty.
isPresenceRequired()  : callable|string|bool
Returns whether a field can be left out.
offsetExists()  : bool
Returns whether an index exists in the rule set
offsetGet()  : ValidationRule
Returns a rule object by its index
offsetSet()  : void
Sets or replace a validation rule
offsetUnset()  : void
Unsets a validation rule
remove()  : $this
Removes a validation rule from the set
requirePresence()  : $this
Sets whether a field is required to be present in data array.
rule()  : ValidationRule|null
Gets a rule for a given name if exists
rules()  : array<string|int, ValidationRule>
Returns all rules for this validation set

Properties

$_allowEmpty

Denotes if a field is allowed to be empty

protected callable|string|bool $_allowEmpty = false

$_validatePresent

Denotes whether the fieldname key must be present in data array

protected callable|string|bool $_validatePresent = false

Methods

add()

Sets a ValidationRule $rule with a $name

public add(string $name, ValidationRule|array<string|int, mixed> $rule) : $this

Example:

     $set
         ->add('notBlank', ['rule' => 'notBlank'])
         ->add('inRange', ['rule' => ['between', 4, 10])
Parameters
$name : string

The name under which the rule should be set

$rule : ValidationRule|array<string|int, mixed>

The validation rule to be set

Return values
$this

allowEmpty()

Sets whether a field value is allowed to be empty.

public allowEmpty(callable|string|bool $allowEmpty) : $this
Parameters
$allowEmpty : callable|string|bool

Valid values are true, false, 'create', 'update' or a callable.

Return values
$this

count()

Returns the number of rules in this set

public count() : int
Return values
int

isEmptyAllowed()

Returns whether a field can be left empty.

public isEmptyAllowed() : callable|string|bool
Return values
callable|string|bool

isPresenceRequired()

Returns whether a field can be left out.

public isPresenceRequired() : callable|string|bool
Return values
callable|string|bool

offsetExists()

Returns whether an index exists in the rule set

public offsetExists(string $index) : bool
Parameters
$index : string

name of the rule

Return values
bool

offsetSet()

Sets or replace a validation rule

public offsetSet(string $index, ValidationRule|array<string|int, mixed> $rule) : void
Parameters
$index : string

name of the rule

$rule : ValidationRule|array<string|int, mixed>

Rule to add to $index

offsetUnset()

Unsets a validation rule

public offsetUnset(string $index) : void
Parameters
$index : string

name of the rule

remove()

Removes a validation rule from the set

public remove(string $name) : $this

Example:

     $set
         ->remove('notBlank')
         ->remove('inRange')
Parameters
$name : string

The name under which the rule should be unset

Return values
$this

requirePresence()

Sets whether a field is required to be present in data array.

public requirePresence(callable|string|bool $validatePresent) : $this
Parameters
$validatePresent : callable|string|bool

Valid values are true, false, 'create', 'update' or a callable.

Return values
$this

        
On this page

Search results