A simple validation component repository has: 1. basic valid rule 2. validation client
$validation = new \Validation\Validator([
'name' => 'foo',
'company' => 'bar',
'type' => 1,
'count' => 10
]);
$validation->setRules([
'name' => 'requried|max:8',
'company' => 'min:3',
'type' => 'required|in:1,2',
'count' => new \Validation\Rule\WhenThen(
'type',
function ($v) {return $v == 1;},
new \Validation\Rule\CallableRule('maxCount', function ($v) {
return $v < 100;
})
)
]);
$result = $validation->validate();
print_r($validation->getErrors());- confirm
one field must equal to another field
$validation->setRule('foo', 'confirm:bar');
- file
file limit
$validation->setRule('file', new File(1024*1024*5, 0, null, true));
- in
field value contain in list
$validation->setRule('bar', 'in:1,2,3');
- max
field length must less than max value
$validation->setRule('bar', 'max:10');
- numeric
field must be a numeric
$validation->setRule('size', 'numeric');
- requried
field must be a required
$validation->setRule('name', 'required');
- whenThen
field valid when closure return true
$validation->setRule('days', new WhenThen('rememberMe', function ($v) {return $v == 'yes';}, 'in:1,3,7'));
- callable rule
callback rule, see CallableRule class
$validation->setRule('agree', function ($v) { if ($v == false) { $this->setError('You must agree the agreement'); return false; } return true; });
$validation->setRule('foo', 'required|in:1,2', [
'required' => 'you miss foo',
'in' => 'foo value must in [1,2]'
]);
you just need implements Validation\Rule\Rule
class MyRule implements Validation\Rule\Rule
{
public function getMessage()
{
return 'myRuleName';
}
public function pass($v, Validation\Validator $context)
{
return $v == 'ok';
}
public function getMessage()
{
return '%s is not equal to "ok"';
}
}- more baisc rules
- message with i18n