diff --git a/README.md b/README.md index f0747c6..88a2719 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,31 @@ $v->rule('min', 'bar', 0); $v->validate(); ``` +You can also add rules on a per-field basis: +```php +$rules = [ + 'required', + ['lengthMin', 4] +]; + +$v = new Valitron\Validator(array('foo' => 'bar')); +$v->mapFieldRules('foo', $rules); +$v->validate(); +``` + +Or for multiple fields at once: + +```php +$rules = [ + 'foo' => ['required', 'integer'], + 'bar'=>['email', ['lengthMin', 4]] +]; + +$v = new Valitron\Validator(array('foo' => 'bar', 'bar' => 'mail@example.com)); +$v->mapFieldsRules($rules); +$v->validate(); +``` + ## Adding field label to messages You can do this in two different ways, you can add a individual label to a rule or an array of all labels for the rules. @@ -308,6 +333,7 @@ $v->validate(); This introduces a new set of tags to your error language file which looks like `{field}`, if you are using a rule like `equals` you can access the second value in the language file by incrementing the field with a value like `{field1}`. + ## Re-use of validation rules You can re-use your validation rules to quickly validate different data with the same rules by using the withData method: diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index dbf2086..1d30e22 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -1208,4 +1208,47 @@ class Validator $clone->_errors = array(); return $clone; } + + /** + * Convenience method to add validation rule(s) by field + * + * @param string field_name + * @param array $rules + */ + public function mapFieldRules($field_name, $rules){ + $me = $this; + + array_map(function($rule) use($field_name, $me){ + + //rule must be an array + $rule = (array)$rule; + + //First element is the name of the rule + $rule_name = array_shift($rule); + + //find a custom message, if any + $message = null; + if (isset($rule['message'])){ + $message = $rule['message']; + unset($rule['message']); + } + //Add the field and additional parameters to the rule + $added = call_user_func_array(array($me, 'rule'), array_merge(array($rule_name, $field_name), $rule)); + if (! empty($message)){ + $added->message($message); + } + }, (array) $rules); + } + + /** + * Convenience method to add validation rule(s) for multiple fields + * + * @param array $rules + */ + public function mapFieldsRules($rules){ + $me = $this; + array_map(function($field_name) use($rules, $me){ + $me->mapFieldRules($field_name, $rules[$field_name]); + }, array_keys($rules)); + } } diff --git a/tests/Valitron/MapRulesTest.php b/tests/Valitron/MapRulesTest.php new file mode 100644 index 0000000..46293e4 --- /dev/null +++ b/tests/Valitron/MapRulesTest.php @@ -0,0 +1,108 @@ +mapFieldRules('myField', $rules); + $this->assertFalse($v->validate()); + $this->assertEquals(2, sizeof($v->errors('myField'))); + + $v2 = new Validator(array('myField' => 'myVal')); + $v2->mapFieldRules('myField', $rules); + $this->assertTrue($v2->validate()); + } + + public function testSingleFieldDot() + { + + $v = new Valitron\Validator(array( + 'settings' => array( + array('threshold' => 50), + array('threshold' => 90) + ) + )); + $v->mapFieldRules('settings.*.threshold', array( + array('max', 50) + )); + + $this->assertFalse($v->validate()); + + } + + public function testMapMultipleFieldsRules() + { + $rules = array( + 'myField1' => array( + 'required', + array('lengthMin', 4) + ), + 'myField2' => array( + 'required', + array('lengthMin', 5) + ) + ); + + $v = new Validator(array( + 'myField1' => 'myVal' + )); + $v->mapFieldsRules($rules); + + $this->assertFalse($v->validate()); + $this->assertFalse($v->errors('myField1')); + $this->assertEquals(2, sizeof($v->errors('myField2'))); + + } + + public function testCustomMessageSingleField() + { + $rules = array( + array('lengthMin', 14, 'message' => 'My Custom Error') + ); + + $v = new Validator(array( + 'myField' => 'myVal' + )); + $v->mapFieldRules('myField', $rules); + $this->assertFalse($v->validate()); + $errors = $v->errors('myField'); + $this->assertEquals('My Custom Error', $errors[0]); + } + + public function testCustomMessageMultipleFields() + { + $rules = array( + 'myField1' => array( + array('lengthMin', 14, 'message'=>'My Custom Error 1') + ), + 'myField2' => array( + array('lengthMin', 14, 'message'=>'My Custom Error 2') + ) + ); + + $v = new Validator(array( + 'myField1' => 'myVal', + 'myField2' => 'myVal', + )); + + $v->mapFieldsRules($rules); + $this->assertFalse($v->validate()); + + $errors1 = $v->errors('myField1'); + $this->assertEquals('My Custom Error 1', $errors1[0]); + + + + $errors2 = $v->errors('myField2'); + $this->assertEquals('My Custom Error 2', $errors2[0]); + } +} \ No newline at end of file