Skip to content

Commit

Permalink
Merge pull request #1368 from rasstislav/fix-textarea-max-length
Browse files Browse the repository at this point in the history
FIX Allow setMaxLength for all field types (TextField, TextareaField)
  • Loading branch information
GuySartorelli authored Feb 19, 2025
2 parents 12d7792 + 7fd8626 commit f49d0c8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion code/Model/EditableFormField/EditableTextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\UserForms\Model\EditableFormField;

use SilverStripe\Core\ClassInfo;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
Expand Down Expand Up @@ -185,7 +186,7 @@ protected function updateFormField($field)
}

if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
if ($field instanceof TextField) {
if (ClassInfo::hasMethod($field, 'setMaxLength')) {
$field->setMaxLength((int) $this->MaxLength);
}
$field->setAttribute('data-rule-maxlength', (int) $this->MaxLength);
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Model/EditableFormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function testLengthRange()
// textarea
$textField->Rows = 3;
$attributes = $textField->getFormField()->getAttributes();
$this->assertFalse(isset($attributes['maxLength']));
$this->assertEquals(20, $attributes['maxlength']);
$this->assertEquals(10, $attributes['data-rule-minlength']);
$this->assertEquals(20, $attributes['data-rule-maxlength']);
}
Expand Down

0 comments on commit f49d0c8

Please sign in to comment.