mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-31 07:01:54 +00:00
mapFieldRules
This commit is contained in:
parent
43c97db077
commit
c8895683f7
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
108
tests/Valitron/MapRulesTest.php
Normal file
108
tests/Valitron/MapRulesTest.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
use Valitron\Validator;
|
||||
|
||||
|
||||
class MapRulesTest extends BaseTestCase
|
||||
{
|
||||
public function testMapSingleFieldRules()
|
||||
{
|
||||
|
||||
$rules = array(
|
||||
'required',
|
||||
array('lengthMin', 4)
|
||||
);
|
||||
|
||||
$v = new Validator(array());
|
||||
$v->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]);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user