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: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133:
<?php
namespace MvcCore\Ext\Forms\Validators;
class Number
extends \MvcCore\Ext\Forms\Validator
implements \MvcCore\Ext\Forms\Fields\IMinMaxStepNumbers {
use \MvcCore\Ext\Forms\Field\Props\MinMaxStepNumbers;
const ERROR_NUMBER = 0;
const ERROR_GREATER = 1;
const ERROR_LOWER = 2;
const ERROR_RANGE = 3;
const ERROR_DIVISIBLE = 4;
protected static $errorMessages = [
self::ERROR_NUMBER => "Field `{0}` requires a valid number.",
self::ERROR_GREATER => "Field `{0}` requires a value equal or greater than `{1}`.",
self::ERROR_LOWER => "Field `{0}` requires a value equal or lower than `{1}`.",
self::ERROR_RANGE => "Field `{0}` requires a value of `{1}` to `{2}` inclusive.",
self::ERROR_DIVISIBLE => "Field `{0}` requires a divisible value of `{1}`.",
];
protected static $fieldSpecificProperties = [
'min' => NULL,
'max' => NULL,
'step' => NULL,
];
public function Validate ($rawSubmittedValue) {
$rawSubmittedValue = (string) $rawSubmittedValue;
if (mb_strlen($rawSubmittedValue) === 0) return NULL;
$result = $this->parseFloat($rawSubmittedValue);
if ($result === NULL) {
$this->field->AddValidationError(
static::GetErrorMessage(static::ERROR_NUMBER)
);
return NULL;
}
if (
$this->min !== NULL && $this->max !== NULL &&
$this->min > 0 && $this->max > 0 &&
($result < $this->min || $result > $this->max)
) {
$this->field->AddValidationError(
$this->form->GetDefaultErrorMsg(static::ERROR_RANGE),
[$this->min, $this->max]
);
} else if ($this->min !== NULL && $this->min > 0 && $result < $this->min) {
$this->field->AddValidationError(
$this->form->GetDefaultErrorMsg(static::ERROR_GREATER),
[$this->min]
);
} else if ($this->max !== NULL && $this->max > 0 && $result > $this->max) {
$this->field->AddValidationError(
$this->form->GetDefaultErrorMsg(static::ERROR_LOWER),
[$this->max]
);
}
if ($this->step !== NULL && $this->step !== 0) {
$dividingResultFloat = floatval($result) / $this->step;
$dividingResultInt = floatval(intval($dividingResultFloat));
if ($dividingResultFloat !== $dividingResultInt)
$this->field->AddValidationError(
$this->form->GetDefaultErrorMsg(static::ERROR_DIVISIBLE),
[$this->step]
);
}
return $result;
}
protected function parseFloat ($rawSubmittedValue) {
$extToolsLocalesFloatParserClass = '\\MvcCore\\Ext\\Tools\\Locales\\FloatParser';
if (!class_exists($extToolsLocalesFloatParserClass)) {
$this->field->AddValidationError(
"MvcCore extension library to parse user input into number "
. "is not installed (`mvccore/ext-tool-locale-floatparser`)."
);
return NULL;
}
$parser = $extToolsLocalesFloatParserClass::CreateInstance()
->SetPreferIntlParsing(FALSE);
if ($formLang = $this->form->GetLang()) $parser->SetLang($formLang);
if ($formLocale = $this->form->GetLocale()) $parser->SetLocale($formLocale);
$result = $parser->Parse($rawSubmittedValue);
return $result;
}
}