Skip to content

Commit

Permalink
Implementing Table::validator() to store a list of validation objects
Browse files Browse the repository at this point in the history
that can be used when saving
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent b582979 commit 6f9f5fa
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 220 deletions.
71 changes: 71 additions & 0 deletions Cake/ORM/Table.php
Expand Up @@ -134,6 +134,13 @@ class Table {
*/
protected $_entityClass;

/**
* A list of validation objects indexed by name
*
* @var array
*/
protected $_validators = [];

/**
* Initializes a new instance
*
Expand Down Expand Up @@ -734,6 +741,70 @@ public function updateAll($fields, $conditions) {
return $success;
}

/**
* 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', 'required', ['rule' => 'email', 'required' => true])
* ->add('password', 'valid', ['rule' => 'notEmpty', 'required' => true]);
* }
* }}}
*
* Otherwise, you can build the object by yourself and store it in the Table object:
*
* {{{
* $validator = new \Cake\ORM\Validator($table);
* $validator
* ->add('email', 'required', ['rule' => 'email', 'required' => true])
* ->add('password', 'valid', ['rule' => 'notEmpty', 'required' => true]);
* $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.
*
* @param string $name the name of the validation set to return
* @param \Cake\ORM\Validator $validator
* @return \Cake\ORM\Validator
*/
public function validator($name = 'default', $instance = null) {
if ($instance === null && isset($this->_validators[$name])) {
return $this->_validators[$name];
}

if ($instance !== null) {
return $this->_validators[$name] = $instance;
}

$validator = new Validator;
$validator = $this->{'validation' . $name}($validator);
return $this->_validators[$name] = $validator;
}

/**
* Returns the default validator object. Subclasses can override this function
* to add a default validation set to the validator object.
*
* @param \Cake\ORM\Validator $validator The validator that can be modified to
* add some rules to it.
* @return \Cake\ORM\Validator
*/
public function validationDefault(Validator $validator) {
return $validator;
}

/**
* Delete all matching rows.
*
Expand Down

0 comments on commit 6f9f5fa

Please sign in to comment.