This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BetweenNumbersValidator.php
81 lines (73 loc) 路 2.13 KB
/
BetweenNumbersValidator.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
* 2017 Romain CANON <romain.hydrocanon@gmail.com>
*
* This file is part of the TYPO3 FormZ project.
* It is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, either
* version 3 of the License, or any later version.
*
* For the full copyright and license information, see:
* http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Romm\Formz\Validation\Validator;
use Romm\Formz\Validation\Field\AbstractFieldValidator;
use TYPO3\CMS\Core\Utility\MathUtility;
class BetweenNumbersValidator extends AbstractFieldValidator
{
const OPTION_MINIMUM = 'minimum';
const OPTION_MAXIMUM = 'maximum';
const MESSAGE_DEFAULT = 'default';
const MESSAGE_NOT_NUMBER = 'notNumber';
/**
* @inheritdoc
*/
protected static $javaScriptValidationFiles = [
'EXT:formz/Resources/Public/JavaScript/Validators/Formz.Validator.BetweenNumbers.js'
];
/**
* @inheritdoc
*/
protected $supportedOptions = [
self::OPTION_MINIMUM => [
0,
'The minimum length to accept',
'integer'
],
self::OPTION_MAXIMUM => [
PHP_INT_MAX,
'The maximum length to accept',
'integer'
]
];
/**
* @inheritdoc
*/
protected $supportedMessages = [
self::MESSAGE_DEFAULT => [
'key' => 'validator.form.between_numbers.error',
'extension' => null
],
self::MESSAGE_NOT_NUMBER => [
'key' => 'validator.form.number.error',
'extension' => null
]
];
/**
* @inheritdoc
*/
public function isValid($value)
{
if (false === MathUtility::canBeInterpretedAsFloat($value)) {
$this->addError(self::MESSAGE_NOT_NUMBER, 1462883487);
} elseif ($value < $this->options['minimum']
|| $value > $this->options['maximum']
) {
$this->addError(
self::MESSAGE_DEFAULT,
1462884916,
[$this->options['minimum'], $this->options['maximum']]
);
}
}
}