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
$_behaviors
protected array
$_fields
protected $_methods
protected array
Holds the available custom callback methods, usually taken from model methods and behavior methods
$_model
protected $_modelMethods
protected 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
Sets the CakeValidationSet objects from the Model::$validate
property If Model::$validate
is not set or empty, this method returns false. True otherwise.
Runs validation for hasAndBelongsToMany associations that have 'with' keys set and data in the data set.
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.
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 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
Gets all possible custom methods from the Model and attached Behaviors to be used as validators
Marks a field as invalid, optionally setting a message explaining why the rule failed
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.
__construct( Model $Model )
Constructor
Model
$Model
_parseRules( )
Sets the CakeValidationSet objects from the Model::$validate
property If Model::$validate
is not set or empty, this method returns false. True otherwise.
Model::$validate
was processed, false otherwise_triggerBeforeValidate( array $options array() )
Propagates beforeValidate event
$options
optional array() _validateWithModels( array $options )
Runs validation for hasAndBelongsToMany associations that have 'with' keys set and data in the data set.
$options
_validationList( array $fieldList array() )
Processes the passed fieldList and returns the list of fields to be validated
$fieldList
optional array() CakeValidationSet[]
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.
$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') ));
$field
CakeValidationSet
$name
CakeValidationRule
$rule
optional null ModelValidator
count( )
Returns the number of fields having validation rules
Countable::count()
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.
$options
optional array() 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
$name
optional null CakeValidationSet
|array|nullgetIterator( )
Returns an iterator for each of the fields to be validated
IteratorAggregate::getIterator()
getMethods( )
Gets all possible custom methods from the Model and attached Behaviors to be used as validators
invalidate( string $field , string $message true )
Marks a field as invalid, optionally setting a message explaining why the rule failed
$field
$message
optional true offsetExists( string $field )
Returns whether a rule set is defined for a field or not
$field
ArrayAccess::offsetExists()
offsetGet( string $field )
Returns the rule set for a field
$field
CakeValidationSet
ArrayAccess::offsetGet()
offsetSet( string $field , array|CakeValidationSet $rules )
Sets the rule set for a field
$field
CakeValidationSet
$rules
ArrayAccess::offsetSet()
offsetUnset( string $field )
Unsets the rule set for a field
$field
ArrayAccess::offsetUnset()
remove( string $field , string $rule null )
Removes a rule from the set by its name
$validator ->remove('title', 'required') ->remove('user_id')
$field
$rule
optional null ModelValidator
setValidationDomain( string $validationDomain null )
Sets the I18n domain for validation messages. This method is chainable.
$validationDomain
optional null ModelValidator
validateAssociated( array $data , array $options array() )
Validates a single record, as well as all its directly associated records.
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
$data
$options
optional array() 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( array $data , array $options array() )
Validates multiple individual records for a single model
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
$data
$options
optional array() 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( 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.
$options
optional array() protected array
Holds the list of behavior names that were attached when this object was created
array()
protected array
Holds the available custom callback methods, usually taken from model methods and behavior methods
array()
protected Model
Holds the reference to the model this Validator is attached to
array()
protected array
Holds the available custom callback methods from the model
array()
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()
© 2005–2016 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/2.9/class-ModelValidator.html