Overview

Packages

  • app
    • Config
      • Schema
    • Test
      • Case
    • View
      • Helper
  • 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
    • Test
      • Case
        • 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
        • Utility
        • View
          • Helper
      • Fixture
      • TestApp
        • Console
          • Command
        • Controller
        • Lib
          • Cache
            • Engine
          • Log
            • Engine
          • Utility
        • Model
          • Behavior
        • Plugin
          • TestPlugin
            • Config
              • Schema
            • Console
              • Command
                • Task
            • Controller
              • Component
            • Lib
              • Cache
                • Engine
              • Custom
                • Package
              • Error
              • Log
                • Engine
            • Model
              • Behavior
            • Vendor
              • sample
            • View
              • Helper
          • TestPluginTwo
            • Console
              • Command
        • Routing
          • Filter
        • Vendor
          • sample
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper
  • None
  • PHP

Classes

  • AclNode
  • Aco
  • AcoAction
  • Aro
  • BehaviorCollection
  • CakeSchema
  • ConnectionManager
  • DbAclSchema
  • FixturePrefixTest
  • I18nModel
  • Model
  • ModelBehavior
  • ModelValidator
  • Permission
  • Overview
  • Package
  • Class
  • Tree
  • Download

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
Copyright: Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
License: MIT License
Link: http://book.cakephp.org/2.0/en/data-validation.html
Located at Model/ModelValidator.php
Methods summary
public
# __construct( Model $Model )

Constructor

Constructor

Parameters

$Model
A reference to the Model the Validator is attached to
public boolean
# 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.

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

$options
An optional array of custom options to be made available in the beforeValidate callback

Returns

boolean
True if there are no errors
public array|boolean
# validateAssociated( array & $data, array $options = array() )

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

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

$data
$data Record data to validate. This should be an array indexed by association name.
$options
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.
public mixed
# validateMany( array & $data, array $options = array() )

Validates multiple individual records for a single model

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

$data
$data Record data to validate. This should be a numerically-indexed array
$options
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.
public array
# 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.

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

$options
An optional array of custom options to be made available in the beforeValidate callback

Returns

array
Array of invalid fields

See

ModelValidator::validates()

Triggers

Model.afterValidate $model
public
# invalidate( string $field, string $message = true )

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

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

Parameters

$field
The name of the field to invalidate
$message
Validation message explaining why the rule failed, defaults to true.
public array
# getMethods( )

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

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
public CakeValidationSet|array|null
# 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

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

$name
[optional] The fieldname to fetch. Defaults to null.

Returns

CakeValidationSet|array|null
CakeValidationSet|array|null
protected boolean
# _parseRules( )

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

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
public mixed
# setValidationDomain( string $validationDomain = null )

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

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

Parameters

$validationDomain
[optional] The validation domain to be used.

Returns

mixed
$this
public Model
# getModel( )

Gets the model related to this validator

Gets the model related to this validator

Returns

Model
Model
protected array
# _validationList( array $fieldList = array() )

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

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

Parameters

$fieldList
list of fields to be used for validation

Returns

array
List of validation rules to be applied
protected boolean
# _validateWithModels( array $options )

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

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

Parameters

$options
Array of options to use on Validation of with models

Returns

boolean
Failure of validation on with models.

See

Model::validates()
protected boolean
# _triggerBeforeValidate( array $options = array() )

Propagates beforeValidate event

Propagates beforeValidate event

Parameters

$options
Options to pass to callback.

Returns

boolean
bool

Triggers

Model.beforeValidate $model, array($options)
public boolean
# offsetExists( string $field )

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

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

Parameters

$field
name of the field to check

Returns

boolean
bool

Implementation of

ArrayAccess::offsetExists()
public CakeValidationSet
# offsetGet( string $field )

Returns the rule set for a field

Returns the rule set for a field

Parameters

$field
name of the field to check

Returns

CakeValidationSet
CakeValidationSet

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( string $field, array|CakeValidationSet $rules )

Sets the rule set for a field

Sets the rule set for a field

Parameters

$field
name of the field to set
$rules
set of rules to apply to field

Implementation of

ArrayAccess::offsetSet()
public
# offsetUnset( string $field )

Unsets the rule set for a field

Unsets the rule set for a field

Parameters

$field
name of the field to unset

Implementation of

ArrayAccess::offsetUnset()
public ArrayIterator
# getIterator( )

Returns an iterator for each of the fields to be validated

Returns an iterator for each of the fields to be validated

Returns

ArrayIterator
ArrayIterator

Implementation of

IteratorAggregate::getIterator()
public integer
# count( )

Returns the number of fields having validation rules

Returns the number of fields having validation rules

Returns

integer
int

Implementation of

Countable::count()
public mixed
# 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.

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' => 'notEmpty', '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

$field
The name of the field where the rule is to be added
$name
name of the rule to be added or list of rules for the field
$rule
or list of rules to be added to the field's rule set

Returns

mixed
$this
public mixed
# remove( string $field, string $rule = null )

Removes a rule from the set by its name

Removes a rule from the set by its name

Example:

` $validator ->remove('title', 'required') ->remove('user_id') `

Parameters

$field
The name of the field from which the rule will be removed
$rule
the name of the rule to be removed

Returns

mixed
$this
Properties summary
protected array $_fields

Holds the CakeValidationSet objects array

Holds the CakeValidationSet objects array

#array()
protected Model $_model

Holds the reference to the model this Validator is attached to

Holds the reference to the model this Validator is attached to

#array()
protected array $_validate

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

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

#array()
protected array $_methods

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

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

#array()
protected array $_modelMethods

Holds the available custom callback methods from the model

Holds the available custom callback methods from the model

#array()
protected array $_behaviors

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

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

#array()
CakePHP API documentation generated by ApiGen 3.0dev