From ca06f000a19968893b400da2cfbffd3dd240990e Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Wed, 16 Jan 2019 23:32:15 +1300 Subject: [PATCH 1/3] FIX Fixes #850 added validation to numerical form fields --- code/Model/EditableFormField/EditableNumericField.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/code/Model/EditableFormField/EditableNumericField.php b/code/Model/EditableFormField/EditableNumericField.php index 9916db9..2190d12 100755 --- a/code/Model/EditableFormField/EditableNumericField.php +++ b/code/Model/EditableFormField/EditableNumericField.php @@ -82,4 +82,12 @@ class EditableNumericField extends EditableFormField $field->setAttribute('data-rule-max', $this->MaxValue); } } + public function validate() + { + $result = parent::validate(); + if ($this->MinValue > $this->MaxValue) { + $result->addError("Minimum length should be less than the Maximum length."); + } + return $result; + } } From d9172625656e1c9e76e1380282bf49fafaf6f574 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Thu, 17 Jan 2019 09:20:31 +1300 Subject: [PATCH 2/3] FIX Fixes #850 Considered location following peer feedback --- code/Model/EditableFormField/EditableNumericField.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/code/Model/EditableFormField/EditableNumericField.php b/code/Model/EditableFormField/EditableNumericField.php index 2190d12..5cd123b 100755 --- a/code/Model/EditableFormField/EditableNumericField.php +++ b/code/Model/EditableFormField/EditableNumericField.php @@ -86,7 +86,10 @@ class EditableNumericField extends EditableFormField { $result = parent::validate(); if ($this->MinValue > $this->MaxValue) { - $result->addError("Minimum length should be less than the Maximum length."); + $result->addError( + + _t(__CLASS__ . '.ORDER_WARNING', 'Minimum length should be less than the maximum length.') + ); } return $result; } From f235c8642cc4f6deb8c706972993bb14b2c6372b Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Wed, 16 Jan 2019 21:59:02 +0100 Subject: [PATCH 3/3] Add unit tests for EditableNumericField::validate() --- .../EditableNumericField.php | 2 +- .../EditableNumericFieldTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/code/Model/EditableFormField/EditableNumericField.php b/code/Model/EditableFormField/EditableNumericField.php index 5cd123b..2fae6e6 100755 --- a/code/Model/EditableFormField/EditableNumericField.php +++ b/code/Model/EditableFormField/EditableNumericField.php @@ -82,12 +82,12 @@ class EditableNumericField extends EditableFormField $field->setAttribute('data-rule-max', $this->MaxValue); } } + public function validate() { $result = parent::validate(); if ($this->MinValue > $this->MaxValue) { $result->addError( - _t(__CLASS__ . '.ORDER_WARNING', 'Minimum length should be less than the maximum length.') ); } diff --git a/tests/Model/EditableFormField/EditableNumericFieldTest.php b/tests/Model/EditableFormField/EditableNumericFieldTest.php index 9b7b8e9..035649c 100644 --- a/tests/Model/EditableFormField/EditableNumericFieldTest.php +++ b/tests/Model/EditableFormField/EditableNumericFieldTest.php @@ -14,4 +14,27 @@ class EditableNumericFieldTest extends SapphireTest $field->Name = 'EditableFormField_123456'; $this->assertEmpty($field->getFormField()->Title()); } + + public function testValidateAddsErrorWhenMinValueIsGreaterThanMaxValue() + { + /** @var EditableNumericField $field */ + $field = EditableNumericField::create(); + $field->MinValue = 10; + $field->MaxValue = 5; + + $result = $field->validate(); + $this->assertFalse($result->isValid(), 'Validation should fail when min is greater than max'); + $this->assertContains('Minimum length should be less than the maximum length', $result->serialize()); + } + + public function testValidate() + { + /** @var EditableNumericField $field */ + $field = EditableNumericField::create(); + $field->MinValue = 5; + $field->MaxValue = 10; + + $result = $field->validate(); + $this->assertTrue($result->isValid()); + } }