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
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.9 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.9
      • 4.2
      • 4.1
      • 4.0
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper
  • None

Classes

  • AclNode
  • Aco
  • AcoAction
  • Aro
  • BehaviorCollection
  • CakeSchema
  • ConnectionManager
  • I18nModel
  • Model
  • ModelBehavior
  • ModelValidator
  • Permission

Class ModelValidator

ModelValidator object encapsulates all methods related to data validations for a model It also provides an API to dynamically change validation rules for each model field.

Implements ArrayAccess to easily modify rules as usually done with Model::$validate definition array

ModelValidator implements ArrayAccess, IteratorAggregate, Countable
Package: Cake\Model
Link: http://book.cakephp.org/2.0/en/data-validation.html
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Location: Cake/Model/ModelValidator.php

Properties summary

  • $_behaviors protected
    array
    Holds the list of behavior names that were attached when this object was created
  • $_fields protected
    CakeValidationSet[]
    Holds the CakeValidationSet objects array
  • $_methods protected
    array

    Holds the available custom callback methods, usually taken from model methods and behavior methods

  • $_model protected
    Model
    Holds the reference to the model this Validator is attached to
  • $_modelMethods protected
    array
    Holds the available custom callback methods from the model
  • $_validate protected
    array

    The validators $validate property, used for checking whether validation rules definition changed in the model and should be refreshed in this class

Method Summary

  • __construct() public
    Constructor
  • _parseRules() protected

    Sets the CakeValidationSet objects from the Model::$validate property If Model::$validate is not set or empty, this method returns false. True otherwise.

  • _triggerBeforeValidate() protected
    Propagates beforeValidate event
  • _validateWithModels() protected

    Runs validation for hasAndBelongsToMany associations that have 'with' keys set and data in the data set.

  • _validationList() protected
    Processes the passed fieldList and returns the list of fields to be validated
  • add() public

    Adds a new rule to a field's rule set. If second argument is an array or instance of CakeValidationSet then rules list for the field will be replaced with second argument and third argument will be ignored.

  • count() public
    Returns the number of fields having validation rules
  • errors() public

    Returns an array of fields that have failed validation. On the current model. This method will actually run validation rules over data, not just return the messages.

  • getField() public

    Returns a CakeValidationSet object containing all validation rules for a field, if no params are passed then it returns an array with all CakeValidationSet objects for each field

  • getIterator() public
    Returns an iterator for each of the fields to be validated
  • getMethods() public

    Gets all possible custom methods from the Model and attached Behaviors to be used as validators

  • getModel() public
    Gets the model related to this validator
  • invalidate() public

    Marks a field as invalid, optionally setting a message explaining why the rule failed

  • offsetExists() public
    Returns whether a rule set is defined for a field or not
  • offsetGet() public
    Returns the rule set for a field
  • offsetSet() public
    Sets the rule set for a field
  • offsetUnset() public
    Unsets the rule set for a field
  • remove() public
    Removes a rule from the set by its name
  • setValidationDomain() public
    Sets the I18n domain for validation messages. This method is chainable.
  • validateAssociated() public
    Validates a single record, as well as all its directly associated records.
  • validateMany() public
    Validates multiple individual records for a single model
  • validates() public

    Returns true if all fields pass validation. Will validate hasAndBelongsToMany associations that use the 'with' key as well. Since Model::_saveMulti is incapable of exiting a save operation.

Method Detail

__construct() public ¶

__construct( Model $Model )

Constructor

Parameters
Model $Model
A reference to the Model the Validator is attached to

_parseRules() protected ¶

_parseRules( )

Sets the CakeValidationSet objects from the Model::$validate property If Model::$validate is not set or empty, this method returns false. True otherwise.

Returns
boolean
true if Model::$validate was processed, false otherwise

_triggerBeforeValidate() protected ¶

_triggerBeforeValidate( array $options = array() )

Propagates beforeValidate event

Parameters
array $options optional array()
Options to pass to callback.
Returns
boolean
Triggers
Model.beforeValidate $model, array($options)

_validateWithModels() protected ¶

_validateWithModels( array $options )

Runs validation for hasAndBelongsToMany associations that have 'with' keys set and data in the data set.

Parameters
array $options
Array of options to use on Validation of with models
Returns
boolean
Failure of validation on with models.
See
Model::validates()

_validationList() protected ¶

_validationList( array $fieldList = array() )

Processes the passed fieldList and returns the list of fields to be validated

Parameters
array $fieldList optional array()
list of fields to be used for validation
Returns
CakeValidationSet[]
List of validation rules to be applied

add() public ¶

add( string $field , string|array|CakeValidationSet $name , array|CakeValidationRule $rule = null )

Adds a new rule to a field's rule set. If second argument is an array or instance of CakeValidationSet then rules list for the field will be replaced with second argument and third argument will be ignored.

Example:

$validator
        ->add('title', 'required', array('rule' => 'notBlank', 'required' => true))
        ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User'))

    $validator->add('password', array(
        'size' => array('rule' => array('lengthBetween', 8, 20)),
        'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid')
    ));
Parameters
string $field
The name of the field where the rule is to be added
string|array|CakeValidationSet $name
name of the rule to be added or list of rules for the field
array|CakeValidationRule $rule optional null
or list of rules to be added to the field's rule set
Returns
ModelValidator

count() public ¶

count( )

Returns the number of fields having validation rules

Returns
integer
Implementation of
Countable::count()

errors() public ¶

errors( string $options = array() )

Returns an array of fields that have failed validation. On the current model. This method will actually run validation rules over data, not just return the messages.

Parameters
string $options optional array()
An optional array of custom options to be made available in the beforeValidate callback
Returns
array
Array of invalid fields
Triggers
Model.afterValidate $model
See
ModelValidator::validates()

getField() public ¶

getField( string $name = null )

Returns a CakeValidationSet object containing all validation rules for a field, if no params are passed then it returns an array with all CakeValidationSet objects for each field

Parameters
string $name optional null
[optional] The fieldname to fetch. Defaults to null.
Returns
CakeValidationSet|array|null

getIterator() public ¶

getIterator( )

Returns an iterator for each of the fields to be validated

Returns
ArrayIterator
Implementation of
IteratorAggregate::getIterator()

getMethods() public ¶

getMethods( )

Gets all possible custom methods from the Model and attached Behaviors to be used as validators

Returns
array
List of callables to be used as validation methods

getModel() public ¶

getModel( )

Gets the model related to this validator

Returns
Model

invalidate() public ¶

invalidate( string $field , string $message = true )

Marks a field as invalid, optionally setting a message explaining why the rule failed

Parameters
string $field
The name of the field to invalidate
string $message optional true
Validation message explaining why the rule failed, defaults to true.

offsetExists() public ¶

offsetExists( string $field )

Returns whether a rule set is defined for a field or not

Parameters
string $field
name of the field to check
Returns
boolean
Implementation of
ArrayAccess::offsetExists()

offsetGet() public ¶

offsetGet( string $field )

Returns the rule set for a field

Parameters
string $field
name of the field to check
Returns
CakeValidationSet
Implementation of
ArrayAccess::offsetGet()

offsetSet() public ¶

offsetSet( string $field , array|CakeValidationSet $rules )

Sets the rule set for a field

Parameters
string $field
name of the field to set
array|CakeValidationSet $rules
set of rules to apply to field
Implementation of
ArrayAccess::offsetSet()

offsetUnset() public ¶

offsetUnset( string $field )

Unsets the rule set for a field

Parameters
string $field
name of the field to unset
Implementation of
ArrayAccess::offsetUnset()

remove() public ¶

remove( string $field , string $rule = null )

Removes a rule from the set by its name

Example:

$validator
        ->remove('title', 'required')
        ->remove('user_id')
Parameters
string $field
The name of the field from which the rule will be removed
string $rule optional null
the name of the rule to be removed
Returns
ModelValidator

setValidationDomain() public ¶

setValidationDomain( string $validationDomain = null )

Sets the I18n domain for validation messages. This method is chainable.

Parameters
string $validationDomain optional null
[optional] The validation domain to be used.
Returns
ModelValidator

validateAssociated() public ¶

validateAssociated( array $data , array $options = array() )

Validates a single record, as well as all its directly associated records.

Options

  • atomic: If true (default), returns boolean. If false returns array.
  • fieldList: Equivalent to the $fieldList parameter in Model::save()
  • deep: If set to true, not only directly associated data , but deeper nested associated data is validated as well.

Warning: This method could potentially change the passed argument $data, If you do not want this to happen, make a copy of $data before passing it to this method

Parameters
array $data
$data Record data to validate. This should be an array indexed by association name.
array $options optional array()
Options to use when validating record data (see above), See also $options of validates().
Returns
array|boolean

If atomic: True on success, or false on failure. Otherwise: array similar to the $data array passed, but values are set to true/false depending on whether each record validated successfully.


validateMany() public ¶

validateMany( array $data , array $options = array() )

Validates multiple individual records for a single model

Options

  • atomic: If true (default), returns boolean. If false returns array.
  • fieldList: Equivalent to the $fieldList parameter in Model::save()
  • deep: If set to true, all associated data will be validated as well.

Warning: This method could potentially change the passed argument $data, If you do not want this to happen, make a copy of $data before passing it to this method

Parameters
array $data
$data Record data to validate. This should be a numerically-indexed array
array $options optional array()
Options to use when validating record data (see above), See also $options of validates().
Returns
mixed

If atomic: True on success, or false on failure. Otherwise: array similar to the $data array passed, but values are set to true/false depending on whether each record validated successfully.


validates() public ¶

validates( array $options = array() )

Returns true if all fields pass validation. Will validate hasAndBelongsToMany associations that use the 'with' key as well. Since Model::_saveMulti is incapable of exiting a save operation.

Will validate the currently set data. Use Model::set() or Model::create() to set the active data.

Parameters
array $options optional array()
An optional array of custom options to be made available in the beforeValidate callback
Returns
boolean
True if there are no errors

Properties detail

$_behaviors ¶

protected array

Holds the list of behavior names that were attached when this object was created

array()

$_fields ¶

protected CakeValidationSet[]

Holds the CakeValidationSet objects array

array()

$_methods ¶

protected array

Holds the available custom callback methods, usually taken from model methods and behavior methods

array()

$_model ¶

protected Model

Holds the reference to the model this Validator is attached to

array()

$_modelMethods ¶

protected array

Holds the available custom callback methods from the model

array()

$_validate ¶

protected array

The validators $validate property, used for checking whether validation rules definition changed in the model and should be refreshed in this class

array()
OpenHub
Rackspace
Rackspace
  • 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
  • Slack
  • Paid Support

Generated using CakePHP API Docs