Fr3nch13/CakePHP Utilities

ValidatorAwareTrait

A trait that provides methods for building and interacting with Validators.

This trait is useful when building ORM like features where the implementing class wants to build and customize a variety of validator instances.

This trait expects that classes including it define three constants:

  • DEFAULT_VALIDATOR - The default validator name.
  • VALIDATOR_PROVIDER_NAME - The provider name the including class is assigned in validators.
  • BUILD_VALIDATOR_EVENT - The name of the event to be triggred when validators are built.

If the including class also implements events the Model.buildValidator event will be triggered when validators are created.

Table of Contents

Properties

$_validatorClass  : string
Validator class.
$_validators  : array<string|int, Validator>
A list of validation objects indexed by name

Methods

getValidator()  : Validator
Returns the validation rules tagged with $name. It is possible to have multiple different named validation sets, this is useful when you need to use varying rules when saving from different routines in your system.
hasValidator()  : bool
Checks whether a validator has been set.
setValidator()  : $this
This method stores a custom validator under the given name.
validationDefault()  : Validator
Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.
createValidator()  : Validator
Creates a validator using a custom method inside your class.
validationMethodExists()  : bool
Checks if validation method exists.

Properties

$_validatorClass

Validator class.

protected string $_validatorClass = \Cake\Validation\Validator::class

Methods

getValidator()

Returns the validation rules tagged with $name. It is possible to have multiple different named validation sets, this is useful when you need to use varying rules when saving from different routines in your system.

public getValidator([string|null $name = null ]) : Validator

If a validator has not been set earlier, this method will build a valiator using a method inside your class.

For example, if you wish to create a validation set called 'forSubscription', you will need to create a method in your Table subclass as follows:

public function validationForSubscription($validator)
{
    return $validator
        ->add('email', 'valid-email', ['rule' => 'email'])
        ->add('password', 'valid', ['rule' => 'notBlank'])
        ->requirePresence('username');
}

$validator = $this->getValidator('forSubscription');

You can implement the method in validationDefault in your Table subclass should you wish to have a validation set that applies in cases where no other set is specified.

If a $name argument has not been provided, the default validator will be returned. You can configure your default validator name in a DEFAULT_VALIDATOR class constant.

Parameters
$name : string|null = null

The name of the validation set to return.

Return values
Validator

hasValidator()

Checks whether a validator has been set.

public hasValidator(string $name) : bool
Parameters
$name : string

The name of a validator.

Return values
bool

setValidator()

This method stores a custom validator under the given name.

public setValidator(string $name, Validator $validator) : $this

You can build the object by yourself and store it in your object:

$validator = new \Cake\Validation\Validator();
$validator
    ->add('email', 'valid-email', ['rule' => 'email'])
    ->add('password', 'valid', ['rule' => 'notBlank'])
    ->allowEmpty('bio');
$this->setValidator('forSubscription', $validator);
Parameters
$name : string

The name of a validator to be set.

$validator : Validator

Validator object to be set.

Return values
$this

validationDefault()

Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.

public validationDefault(Validator $validator) : Validator
Parameters
$validator : Validator

The validator that can be modified to add some rules to it.

Return values
Validator

createValidator()

Creates a validator using a custom method inside your class.

protected createValidator(string $name) : Validator

This method is used only to build a new validator and it does not store it in your object. If you want to build and reuse validators, use getValidator() method instead.

Parameters
$name : string

The name of the validation set to create.

Tags
throws
RuntimeException
Return values
Validator

validationMethodExists()

Checks if validation method exists.

protected validationMethodExists(string $name) : bool
Parameters
$name : string

Validation method name.

Return values
bool

        
On this page

Search results