Skip to content

Commit

Permalink
Merge branch '4' into 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 2, 2023
2 parents 15eade8 + a67b4bd commit a0493cf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Forms/TextareaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public function getAttributes()
return $attributes;
}


/**
* {@inheritdoc}
*/
Expand All @@ -167,6 +166,41 @@ public function Type()
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
*
Expand Down
25 changes: 25 additions & 0 deletions tests/php/Forms/TextareaFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\RequiredFields;

class TextareaFieldTest extends SapphireTest
{
Expand Down Expand Up @@ -50,6 +51,30 @@ public function testReadonlyDisplaySpecialHTML()
);
}

/**
* 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 <b>are</b> some unicodes: äöü";
Expand Down

0 comments on commit a0493cf

Please sign in to comment.