forked from braska/SimpleBannerRotator
/
MaxMinValidator.php
37 lines (29 loc) · 1018 Bytes
/
MaxMinValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
namespace App\Library\Validators;
use Phalcon\Validation\Validator,
Phalcon\Validation\ValidatorInterface,
Phalcon\Validation\Message;
class MaxMinValidator extends Validator implements ValidatorInterface
{
public function validate(\Phalcon\Validation $validator, $attribute)
{
$allowZero = $this->getOption('allowZero');
$min = $this->getOption('min');
$max = $this->getOption('max');
$field = $this->getOption('field');
$value = $validator->getValue($attribute);
$len = mb_strlen($value, 'utf-8');
if ($len > $max || $len < $min) {
if(!empty($value)) {
$validator->appendMessage(new Message(
"Длина поля \"{$field}\" должна быть от $min до $max",
$field,
"MaxMinValidator"
));
return false;
}
elseif($allowZero) return true;
}
return true;
}
}