CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 4.6 Strawberry API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 4.6
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

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 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.

Namespace: Cake\Validation

Property Summary

  • $_validatorClass protected
    string

    Validator class.

  • $_validators protected
    array<Cake\Validation\Validator>

    A list of validation objects indexed by name

Method Summary

  • createValidator() protected

    Creates a validator using a custom method inside your class.

  • getValidator() 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.

  • hasValidator() public

    Checks whether a validator has been set.

  • setValidator() public

    This method stores a custom validator under the given name.

  • validationDefault() public

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

  • validationMethodExists() protected

    Checks if validation method exists.

Method Detail

createValidator() ¶ protected

createValidator(string $name): Cake\Validation\Validator

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() ¶ public

getValidator(string|null $name = null): Cake\Validation\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.

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

The name of the validation set to return.

Returns
Cake\Validation\Validator

hasValidator() ¶ public

hasValidator(string $name): bool

Checks whether a validator has been set.

Parameters
string $name

The name of a validator.

Returns
bool

setValidator() ¶ public

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

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();
$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() ¶ public

validationDefault(Cake\Validation\Validator $validator): Cake\Validation\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() ¶ protected

validationMethodExists(string $name): bool

Checks if validation method exists.

Parameters
string $name

Validation method name.

Returns
bool

Property Detail

$_validatorClass ¶ protected

Validator class.

Type
string

$_validators ¶ protected

A list of validation objects indexed by name

Type
array<Cake\Validation\Validator>
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs