Trait 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 two constants:

  • DEFAULT_VALIDATOR - The default validator name.
  • VALIDATOR_PROVIDER_NAME - The provider name the including class is assigned in validators.

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

Direct Users

Properties summary

Method Summary

  • createValidator() protected
    Creates a validator using a custom method inside your class.
  • 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.

  • Checks whether or not a validator has been set.
  • This method stores a custom validator under the given name.
  • Returns the default validator object. Subclasses can override this function to add a default validation set to the validator object.

  • Checks if validation method exists.
  • validator() public

    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.

Method Detail

createValidator()source protected

createValidator( string $name )

Creates a validator using a custom method inside your class.

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

string $name
The name of the validation set to create.

Returns

Cake\Validation\Validator

Throws

RuntimeException

getValidator()source public

getValidator( string|null $name null )

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.

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

string|null $name optional null
The name of the validation set to return.

Returns

Cake\Validation\Validator

hasValidator()source public

hasValidator( string $name )

Checks whether or not a validator has been set.

Parameters

string $name
The name of a validator.

Returns

boolean

setValidator()source public

setValidator( string $name , Cake\Validation\Validator $validator )

This method stores a custom validator under the given name.

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

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

Parameters

string $name
The name of a validator to be set.
Cake\Validation\Validator $validator
Validator object to be set.

Returns


$this

validationDefault()source public

validationDefault( Cake\Validation\Validator $validator )

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

Parameters

Cake\Validation\Validator $validator

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

Returns

Cake\Validation\Validator

validationMethodExists()source protected

validationMethodExists( string $name )

Checks if validation method exists.

Parameters

string $name
Validation method name.

Returns

boolean

validator()source public

validator( string|null $name null , Cake\Validation\Validator $validator null )

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.

There are two different ways of creating and naming validation sets: by creating a new method inside your own Table subclass, or by building the validator object yourself and storing it using this method.

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');
}

Otherwise, you can build the object by yourself and store it in the Table object:

$validator = new \Cake\Validation\Validator($table);
$validator
 ->add('email', 'valid-email', ['rule' => 'email'])
 ->add('password', 'valid', ['rule' => 'notBlank'])
 ->allowEmpty('bio');
$table->validator('forSubscription', $validator);

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.

Deprecated

3.5.0 Use getValidator/setValidator instead.

Parameters

string|null $name optional null
the name of the validation set to return
Cake\Validation\Validator $validator optional null

The validator instance to store, use null to get a validator.

Returns

Cake\Validation\Validator

Throws

RuntimeException

Properties detail

$_validatorClasssource

protected string

Validator class.

'\Cake\Validation\Validator'

$_validatorssource

protected Cake\Validation\Validator[]

A list of validation objects indexed by name

[]

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Validation.ValidatorAwareTrait.html