| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
- /* * ********************************************************************
- * ProxmoxAddon product developed. (Aug 23, 2018)
- * *
- *
- * CREATED BY MODULESGARDEN -> http://modulesgarden.com
- * CONTACT -> contact@modulesgarden.com
- *
- *
- * This software is furnished under a license and may be used and copied
- * only in accordance with the terms of such license and with the
- * inclusion of the above copyright notice. This software or any other
- * copies thereof may not be provided or otherwise made available to any
- * other person. No title to and ownership of the software is hereby
- * transferred.
- *
- *
- * ******************************************************************** */
- namespace ModulesGarden\ProxmoxAddon\App\UI\Validators;
- use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Validators\BaseValidator;
- /**
- * Description of NumberValidator
- *
- * @author Pawel Kopec <pawelk@modulesgardne.com>
- */
- class NumberValidator extends BaseValidator
- {
- protected $minValue = 0;
- protected $maxValue = 0;
- protected $required = false;
- public function __construct($min = 0, $max = 0, $required = false)
- {
- $this->minValue = (int)$min;
- $this->maxValue = $max;
- $this->required = $required;
- }
- protected function validate($data, $additionalData = null)
- {
- if (!$this->required && empty($data))
- {
- return true;
- }
- if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
- {
- return true;
- }
- //Min & Max
- if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
- {
- return true;
- }
- //Min
- else
- {
- if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
- {
- return true;
- }
- }
- if ($this->minValue === $this->maxValue)
- {
- $this->addValidationError('PleaseProvideANumericValue');
- return false;
- }
- if (is_numeric($this->minValue) && is_numeric($this->maxValue))
- {
- $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
- }
- else
- {
- if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
- {
- $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
- }
- }
- return false;
- }
- }
|