Files
kiri-validator/EnumValidator.php
T

35 lines
688 B
PHP
Raw Normal View History

2022-01-09 14:01:11 +08:00
<?php
declare(strict_types=1);
namespace validator;
/**
* Class EnumValidator
* @package validator
*/
class EnumValidator extends BaseValidator
{
public array $value = [];
/**
* @return bool
*/
public function trigger(): bool
{
2022-02-18 17:16:45 +08:00
return $this->_validator($this->field, function ($field, $params, $values) {
$value = $params[$field] ?? null;
if (empty($value)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field,'The param :attribute is null');
2022-02-18 17:16:45 +08:00
}
if (!in_array($value, $values)) {
2022-02-25 17:12:15 +08:00
return $this->addError($field,'The param :attribute value only in ' . implode(',', $values));
2022-02-18 17:16:45 +08:00
}
return true;
}, $this->params, $this->value);
2022-01-09 14:01:11 +08:00
}
}