@@ 17-51 (lines=35) @@ | ||
14 | * |
|
15 | * This is useful for e.g. date or date/time input which may be parsed by {@see DateTimeStringField}. |
|
16 | */ |
|
17 | class CheckParser implements ValidatorInterface |
|
18 | { |
|
19 | /** |
|
20 | * @var ParserInterface |
|
21 | */ |
|
22 | private $parser; |
|
23 | ||
24 | /** |
|
25 | * @var string |
|
26 | */ |
|
27 | private $error_id; |
|
28 | ||
29 | /** |
|
30 | * @param ParserInterface $parser the InputParser (usually a Field) which should attempt to parse the input |
|
31 | * @param string $error_id error message translation key |
|
32 | */ |
|
33 | public function __construct(ParserInterface $parser, $error_id) |
|
34 | { |
|
35 | $this->parser = $parser; |
|
36 | $this->error_id = $error_id; |
|
37 | } |
|
38 | ||
39 | public function validate(FieldInterface $field, InputModel $model, InputValidation $validation) |
|
40 | { |
|
41 | $input = $model->getInput($field); |
|
42 | ||
43 | if ($input === null) { |
|
44 | return; // no input |
|
45 | } |
|
46 | ||
47 | if ($this->parser->parseInput($input) === null) { |
|
48 | $model->setError($field, lang::text("mindplay/kissform", $this->error_id, ["field" => $validation->getLabel($field)])); |
|
49 | } |
|
50 | } |
|
51 | } |
|
52 |
@@ 17-54 (lines=38) @@ | ||
14 | * This will automatically respect {@link Field::$required} so there is no need to manually |
|
15 | * validate as required() beforehand. |
|
16 | */ |
|
17 | class CheckSelected implements ValidatorInterface |
|
18 | { |
|
19 | /** |
|
20 | * @var string[]|null |
|
21 | */ |
|
22 | private $options; |
|
23 | ||
24 | /** |
|
25 | * @var string|null |
|
26 | */ |
|
27 | private $error; |
|
28 | ||
29 | /** |
|
30 | * @param string[]|null $options list of allowed values |
|
31 | * @param string|null $error optional custom error message |
|
32 | */ |
|
33 | public function __construct(array $options, $error = null) |
|
34 | { |
|
35 | $this->options = $options; |
|
36 | $this->error = $error; |
|
37 | } |
|
38 | ||
39 | public function validate(FieldInterface $field, InputModel $model, InputValidation $validation) |
|
40 | { |
|
41 | $input = $model->getInput($field); |
|
42 | ||
43 | if (($field->isRequired() === false) && ($input === null)) { |
|
44 | return; // no input, not required |
|
45 | } |
|
46 | ||
47 | if (! in_array($input, $this->options)) { |
|
48 | $model->setError( |
|
49 | $field, |
|
50 | $this->error ?: lang::text("mindplay/kissform", "selected", ["field" => $validation->getLabel($field)]) |
|
51 | ); |
|
52 | } |
|
53 | } |
|
54 | } |
|
55 |