From d6c39e7d367c290df4cc5d838e4b1740f11e3d2d Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Mon, 28 Aug 2023 10:51:43 +0200 Subject: [PATCH 1/2] added missing maxLength validation --- src/Forms/TextareaField.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Forms/TextareaField.php b/src/Forms/TextareaField.php index 0a0f69c98..0ffbc23a1 100644 --- a/src/Forms/TextareaField.php +++ b/src/Forms/TextareaField.php @@ -152,7 +152,6 @@ class TextareaField extends FormField return $attributes; } - /** * {@inheritdoc} */ @@ -167,6 +166,41 @@ class TextareaField extends FormField return $parent; } + /** + * Validate this field + * + * @param Validator $validator + * @return bool + */ + public function validate($validator) + { + if (!is_null($this->maxLength) && mb_strlen($this->value ?? '') > $this->maxLength) { + $name = strip_tags($this->Title() ? $this->Title() : $this->getName()); + $validator->validationError( + $this->name, + _t( + 'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH', + 'The value for {name} must not exceed {maxLength} characters in length', + ['name' => $name, 'maxLength' => $this->maxLength] + ), + "validation" + ); + return false; + } + return true; + } + + public function getSchemaValidation() + { + $rules = parent::getSchemaValidation(); + if ($this->getMaxLength()) { + $rules['max'] = [ + 'length' => $this->getMaxLength(), + ]; + } + return $rules; + } + /** * Return value with all values encoded in html entities * From 7ae7e9ff472b5fa9789dea49ebccbc1a829ba419 Mon Sep 17 00:00:00 2001 From: Rastislav Brandobur Date: Wed, 30 Aug 2023 10:56:21 +0200 Subject: [PATCH 2/2] added maxLength validation tests --- tests/php/Forms/TextareaFieldTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/php/Forms/TextareaFieldTest.php b/tests/php/Forms/TextareaFieldTest.php index accd6a587..f75bd4589 100644 --- a/tests/php/Forms/TextareaFieldTest.php +++ b/tests/php/Forms/TextareaFieldTest.php @@ -4,6 +4,7 @@ namespace SilverStripe\Forms\Tests; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\TextareaField; +use SilverStripe\Forms\RequiredFields; class TextareaFieldTest extends SapphireTest { @@ -50,6 +51,30 @@ class TextareaFieldTest extends SapphireTest ); } + /** + * Tests the TextareaField Max Length Validation Failure + */ + public function testMaxLengthValidationFail() + { + $field = new TextareaField("Test", "Test"); + $field->setMaxLength(5); + $field->setValue("John Doe"); // 8 characters, so should fail + $result = $field->validate(new RequiredFields()); + $this->assertFalse($result); + } + + /** + * Tests the TextareaField Max Length Validation Success + */ + public function testMaxLengthValidationSuccess() + { + $field = new TextareaField("Test", "Test"); + $field->setMaxLength(5); + $field->setValue("John"); // 4 characters, so should pass + $result = $field->validate(new RequiredFields()); + $this->assertTrue($result); + } + public function testValueEntities() { $inputText = "These are some unicodes: äöü";