From 6bc507340461b68f5ecc2cc4e78488dc8f6a9052 Mon Sep 17 00:00:00 2001 From: Julian Bennecker Date: Tue, 21 May 2019 10:24:27 +0200 Subject: [PATCH 1/4] Validate numeric min/max values on server side --- code/formfields/UserFormsNumericField.php | 70 +++++++++++++++++++ .../EditableNumericField.php | 12 +++- 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 code/formfields/UserFormsNumericField.php diff --git a/code/formfields/UserFormsNumericField.php b/code/formfields/UserFormsNumericField.php new file mode 100644 index 0000000..fabd00b --- /dev/null +++ b/code/formfields/UserFormsNumericField.php @@ -0,0 +1,70 @@ +min; + } + + /** + * @param mixed $min + */ + public function setMin($min) + { + if (!is_numeric($min)) { + throw new Exception('$min must be a number.'); + } + + $this->min = $min; + } + + /** + * @return mixed + */ + public function getMax() + { + return $this->max; + } + + /** + * @param mixed $max + */ + public function setMax($max) + { + if (!is_numeric($max)) { + throw new Exception('$min must be a number.'); + } + $this->max = $max; + } + + 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)) { + $validator->validationError( + $this->name, + sprintf("This must be between %s and %s", $this->min, $this->max), + "validation", + false + ); + + return false; + } + + return true; + } +} diff --git a/code/model/editableformfields/EditableNumericField.php b/code/model/editableformfields/EditableNumericField.php index c2a1334..0c1c684 100755 --- a/code/model/editableformfields/EditableNumericField.php +++ b/code/model/editableformfields/EditableNumericField.php @@ -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; From f213eecaf980b6edf80162d8409b967d6936fd2f Mon Sep 17 00:00:00 2001 From: Julian Bennecker Date: Fri, 12 Jul 2019 08:53:08 +0200 Subject: [PATCH 2/4] translatable error message --- code/formfields/UserFormsNumericField.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/code/formfields/UserFormsNumericField.php b/code/formfields/UserFormsNumericField.php index fabd00b..a074f4b 100644 --- a/code/formfields/UserFormsNumericField.php +++ b/code/formfields/UserFormsNumericField.php @@ -57,7 +57,11 @@ class UserFormsNumericField extends NumericField if ((!empty($this->min) && $this->value < $this->min) || (!empty($this->max) && $this->value > $this->max)) { $validator->validationError( $this->name, - sprintf("This must be between %s and %s", $this->min, $this->max), + _t( + 'UserFormsNumericField.ErrorMsg', + "This must be between {min} and {max}", + ['min' => $this->min, 'max' => $this->max] + ), "validation", false ); From 7bf85efba95ffe96d6c2ab078dbdf3ee886b0276 Mon Sep 17 00:00:00 2001 From: Julian Bennecker Date: Mon, 15 Jul 2019 11:42:03 +0200 Subject: [PATCH 3/4] Change error messages for UserFormsNumericField --- code/formfields/UserFormsNumericField.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/code/formfields/UserFormsNumericField.php b/code/formfields/UserFormsNumericField.php index a074f4b..984d785 100644 --- a/code/formfields/UserFormsNumericField.php +++ b/code/formfields/UserFormsNumericField.php @@ -55,16 +55,10 @@ class UserFormsNumericField extends NumericField } if ((!empty($this->min) && $this->value < $this->min) || (!empty($this->max) && $this->value > $this->max)) { - $validator->validationError( - $this->name, - _t( - 'UserFormsNumericField.ErrorMsg', - "This must be between {min} and {max}", - ['min' => $this->min, 'max' => $this->max] - ), - "validation", - false - ); + $msg = (!empty($this->min) && $this->value < $this->min) ? + _t(self::class . 'RANGE_ERROR_MIN', 'Please enter a value that is no less than {min}.', $this->min) : + _t(self::class . 'RANGE_ERROR_MAX', 'Please enter a value that is no more than {max}.', $this->max); + $validator->validationError($this->name, $msg, "validation", false); return false; } From cafb4300a78d618f7037a29babc39cc28ae25f3c Mon Sep 17 00:00:00 2001 From: Julian Bennecker Date: Mon, 15 Jul 2019 13:09:11 +0200 Subject: [PATCH 4/4] Some minor tidy up changes --- code/formfields/UserFormsNumericField.php | 26 +++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/code/formfields/UserFormsNumericField.php b/code/formfields/UserFormsNumericField.php index 984d785..15603aa 100644 --- a/code/formfields/UserFormsNumericField.php +++ b/code/formfields/UserFormsNumericField.php @@ -4,12 +4,18 @@ class UserFormsNumericField extends NumericField { + /** + * @var int + */ private $min; + /** + * @var int + */ private $max; /** - * @return mixed + * @return int|null */ public function getMin() { @@ -17,7 +23,9 @@ class UserFormsNumericField extends NumericField } /** - * @param mixed $min + * @param int $min + * @return $this + * @throws Exception If the provided argument is not numeric */ public function setMin($min) { @@ -26,10 +34,11 @@ class UserFormsNumericField extends NumericField } $this->min = $min; + return $this; } /** - * @return mixed + * @return int|null */ public function getMax() { @@ -37,14 +46,17 @@ class UserFormsNumericField extends NumericField } /** - * @param mixed $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('$min must be a number.'); + throw new Exception('$max must be a number.'); } $this->max = $max; + return $this; } public function validate($validator) @@ -56,8 +68,8 @@ class UserFormsNumericField extends NumericField 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}.', $this->min) : - _t(self::class . 'RANGE_ERROR_MAX', 'Please enter a value that is no more than {max}.', $this->max); + _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;