Browse Source

Adding the field name that is being validated as part of the validation
context

Jose Lorenzo Rodriguez 12 years ago
parent
commit
4515475729

+ 1 - 0
src/Validation/ValidationRule.php

@@ -101,6 +101,7 @@ class ValidationRule {
  * - newRecord: (boolean) whether or not the data to be validated belongs to a
  *   new record
  * - data: The full data that was passed to the validation process
+ * - field: The name of the field that is being processed
  * @return bool|string
  * @throws \InvalidArgumentException when the supplied rule is not a valid
  * callable for the configured scope

+ 5 - 4
src/Validation/Validator.php

@@ -100,7 +100,7 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
 				continue;
 			}
 
-			$result = $this->_processRules($field, $data[$name], $data, $newRecord);
+			$result = $this->_processRules($name, $field, $data, $newRecord);
 			if ($result) {
 				$errors[$name] = $result;
 			}
@@ -434,18 +434,19 @@ class Validator implements \ArrayAccess, \IteratorAggregate, \Countable {
  * Iterates over each rule in the validation set and collects the errors resulting
  * from executing them
  *
+ * @param string $field The name of the field that is being processed
  * @param ValidationSet $rules the list of rules for a field
- * @param mixed $value The value to be checked
  * @param array $data the full data passed to the validator
  * @param bool $newRecord whether is it a new record or an existing one
  * @return array
  */
-	protected function _processRules(ValidationSet $rules, $value, $data, $newRecord) {
+	protected function _processRules($field, ValidationSet $rules, $data, $newRecord) {
+		$value = $data[$field];
 		$errors = [];
 		// Loading default provider in case there is none
 		$this->provider('default');
 		foreach ($rules as $name => $rule) {
-			$result = $rule->process($value, $this->_providers, compact('newRecord', 'data'));
+			$result = $rule->process($value, $this->_providers, compact('newRecord', 'data', 'field'));
 			if ($result === true) {
 				continue;
 			}

+ 4 - 2
tests/TestCase/Validation/ValidatorTest.php

@@ -406,7 +406,8 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 					'data' => [
 						'email' => '!',
 						'title' => 'bar'
-					]
+					],
+					'field' => 'title'
 				];
 				$this->assertEquals($expected, $context);
 				return "That ain't cool, yo";
@@ -449,7 +450,8 @@ class ValidatorTest extends \Cake\TestSuite\TestCase {
 					'data' => [
 						'email' => '!',
 						'title' => 'bar'
-					]
+					],
+					'field' => 'title'
 				];
 				$this->assertEquals($expected, $context);
 				return "That ain't cool, yo";