Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementing validator events, to enable conditional validation. To u…

…se conditional validation in models, add `'on' => 'create'` or `'on' => 'update'` to validation rules.
  • Loading branch information...
commit e86fd736f9d31422165773fadf7f070340ffb7d7 1 parent 15dc58f
@nateabele nateabele authored
View
5 libraries/lithium/data/Model.php
@@ -732,7 +732,10 @@ public function save($entity, $data = null, array $options = array()) {
* @return boolean Success.
*/
public function validates($entity, array $options = array()) {
- $defaults = array('rules' => $this->validates);
+ $defaults = array(
+ 'rules' => $this->validates,
+ 'events' => $entity->exists() ? 'update' : 'create',
+ );
$options += $defaults;
$self = static::_object();
$validator = static::$_classes['validator'];
View
35 libraries/lithium/tests/cases/util/ValidatorTest.php
@@ -1033,6 +1033,41 @@ public function testValidationWithContextData() {
);
$this->assertIdentical(array('title' => array(0)), $result);
}
+
+ /**
+ * Tests that event flags applied to rules only trigger when the corresponding event is passed
+ * in the `$options` parameter of `check()`.
+ *
+ * @return void
+ */
+ public function testEvents() {
+ $rules = array('number' => array('numeric', 'message' => 'Badness!'));
+ $expected = array('number' => array('Badness!'));
+
+ $result = Validator::check(array('number' => 'o'), $rules);
+ $this->assertEqual($expected, $result);
+
+ $rules['number']['on'] = 'foo';
+ $result = Validator::check(array('number' => 'o'), $rules, array('events' => 'foo'));
+ $this->assertEqual($expected, $result);
+
+ $result = Validator::check(array('number' => 'o'), $rules, array('events' => 'bar'));
+ $this->assertEqual(array(), $result);
+
+ $result = Validator::check(array('number' => 'o'), $rules, array(
+ 'events' => array('foo', 'bar')
+ ));
+ $this->assertEqual($expected, $result);
+
+ $result = Validator::check(array('number' => 'o'), $rules, array(
+ 'events' => array('bar', 'baz')
+ ));
+ $this->assertEqual(array(), $result);
+
+ unset($rules['number']['on']);
+ $result = Validator::check(array('number' => 'o'), $rules, array('events' => 'foo'));
+ $this->assertEqual($expected, $result);
+ }
}
?>
View
7 libraries/lithium/util/Validator.php
@@ -429,19 +429,24 @@ public static function check(array $values, array $rules, array $options = array
'required' => true,
'skipEmpty' => false,
'format' => 'any',
+ 'on' => null,
);
$errors = array();
+ $events = (array) (isset($options['events']) ? $options['events'] : null);
foreach ($rules as $field => $rules) {
$rules = is_string($rules) ? array('message' => $rules) : $rules;
$rules = is_array(current($rules)) ? $rules : array($rules);
$errors[$field] = array();
-
$options['field'] = $field;
+
foreach ($rules as $key => $rule) {
$rule += $defaults + compact('values');
list($name) = $rule;
+ if ($events && $rule['on'] && !array_intersect($events, (array) $rule['on'])) {
+ continue;
+ }
if (!isset($values[$field])) {
if ($rule['required']) {
$errors[$field][] = $rule['message'] ?: $key;
Please sign in to comment.
Something went wrong with that request. Please try again.