mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
Merge pull request #890 from jbennecker/4.6
Validate numeric min/max values on server side
This commit is contained in:
commit
069cf82468
80
code/formfields/UserFormsNumericField.php
Normal file
80
code/formfields/UserFormsNumericField.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
|
||||
class UserFormsNumericField extends NumericField
|
||||
{
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $min;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $max;
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getMin()
|
||||
{
|
||||
return $this->min;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $min
|
||||
* @return $this
|
||||
* @throws Exception If the provided argument is not numeric
|
||||
*/
|
||||
public function setMin($min)
|
||||
{
|
||||
if (!is_numeric($min)) {
|
||||
throw new Exception('$min must be a number.');
|
||||
}
|
||||
|
||||
$this->min = $min;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getMax()
|
||||
{
|
||||
return $this->max;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $max
|
||||
* @return $this
|
||||
* @throws Exception If the provided argument is not numeric
|
||||
*/
|
||||
public function setMax($max)
|
||||
{
|
||||
if (!is_numeric($max)) {
|
||||
throw new Exception('$max must be a number.');
|
||||
}
|
||||
$this->max = $max;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function validate($validator)
|
||||
{
|
||||
$isValid = parent::validate($validator);
|
||||
if (!$isValid) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((!empty($this->min) && $this->value < $this->min) || (!empty($this->max) && $this->value > $this->max)) {
|
||||
$msg = (!empty($this->min) && $this->value < $this->min) ?
|
||||
_t(self::class . 'RANGE_ERROR_MIN', 'Please enter a value that is no less than {min}.', ['min' => $this->getMin()]) :
|
||||
_t(self::class . 'RANGE_ERROR_MAX', 'Please enter a value that is no more than {max}.', ['max' => $this->getMax()]);
|
||||
$validator->validationError($this->name, $msg, "validation", false);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -27,15 +27,23 @@ class EditableNumericField extends EditableFormField
|
||||
}
|
||||
|
||||
/**
|
||||
* @return NumericField
|
||||
* @return UserFormsNumericField
|
||||
*/
|
||||
public function getFormField()
|
||||
{
|
||||
$field = NumericField::create($this->Name, $this->EscapedTitle, $this->Default)
|
||||
$field = UserFormsNumericField::create($this->Name, $this->EscapedTitle, $this->Default)
|
||||
->setFieldHolderTemplate('UserFormsField_holder')
|
||||
->setTemplate('UserFormsField')
|
||||
->addExtraClass('number');
|
||||
|
||||
if (!empty($this->MinValue)) {
|
||||
$field->setMin($this->MinValue);
|
||||
}
|
||||
|
||||
if (!empty($this->MaxValue)) {
|
||||
$field->setMax($this->MaxValue);
|
||||
}
|
||||
|
||||
$this->doUpdateFormField($field);
|
||||
|
||||
return $field;
|
||||
|
Loading…
x
Reference in New Issue
Block a user