mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
Merge pull request #476 from tractorcow/pulls/fix-maxlength
BUG Fix issue with maxlength="0"
This commit is contained in:
commit
c2b9342aa2
@ -79,7 +79,7 @@ class EditableTextField extends EditableFormField {
|
|||||||
->setTemplate('UserFormsTextareaField')
|
->setTemplate('UserFormsTextareaField')
|
||||||
->setRows($this->Rows);
|
->setRows($this->Rows);
|
||||||
} else {
|
} else {
|
||||||
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default, $this->MaxLength)
|
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default)
|
||||||
->setFieldHolderTemplate('UserFormsField_holder')
|
->setFieldHolderTemplate('UserFormsField_holder')
|
||||||
->setTemplate('UserFormsField');
|
->setTemplate('UserFormsField');
|
||||||
}
|
}
|
||||||
@ -97,12 +97,15 @@ class EditableTextField extends EditableFormField {
|
|||||||
protected function updateFormField($field) {
|
protected function updateFormField($field) {
|
||||||
parent::updateFormField($field);
|
parent::updateFormField($field);
|
||||||
|
|
||||||
if($this->MinLength) {
|
if(is_numeric($this->MinLength) && $this->MinLength > 0) {
|
||||||
$field->setAttribute('data-rule-minlength', $this->MinLength);
|
$field->setAttribute('data-rule-minlength', intval($this->MinLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->MaxLength) {
|
if(is_numeric($this->MaxLength) && $this->MaxLength > 0) {
|
||||||
$field->setAttribute('data-rule-maxlength', $this->MaxLength);
|
if($field instanceof TextField) {
|
||||||
|
$field->setMaxLength(intval($this->MaxLength));
|
||||||
|
}
|
||||||
|
$field->setAttribute('data-rule-maxlength', intval($this->MaxLength));
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->Placeholder) {
|
if($this->Placeholder) {
|
||||||
|
@ -144,4 +144,35 @@ class EditableFormFieldTest extends FunctionalTest {
|
|||||||
$this->assertNotEquals($textfield1->Name, $textfield2->Name);
|
$this->assertNotEquals($textfield1->Name, $textfield2->Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testLengthRange() {
|
||||||
|
/** @var EditableTextField $textField */
|
||||||
|
$textField = $this->objFromFixture('EditableTextField', 'basic-text');
|
||||||
|
|
||||||
|
// Empty range
|
||||||
|
/** @var TextField $formField */
|
||||||
|
$textField->MinLength = 0;
|
||||||
|
$textField->MaxLength = 0;
|
||||||
|
$attributes = $textField->getFormField()->getAttributes();
|
||||||
|
$this->assertFalse(isset($attributes['maxLength']));
|
||||||
|
$this->assertFalse(isset($attributes['data-rule-minlength']));
|
||||||
|
$this->assertFalse(isset($attributes['data-rule-maxlength']));
|
||||||
|
|
||||||
|
// Test valid range
|
||||||
|
$textField->MinLength = 10;
|
||||||
|
$textField->MaxLength = 20;
|
||||||
|
$attributes = $textField->getFormField()->getAttributes();
|
||||||
|
$this->assertEquals(20, $attributes['maxLength']);
|
||||||
|
$this->assertEquals(20, $attributes['size']);
|
||||||
|
$this->assertEquals(10, $attributes['data-rule-minlength']);
|
||||||
|
$this->assertEquals(20, $attributes['data-rule-maxlength']);
|
||||||
|
|
||||||
|
// textarea
|
||||||
|
$textField->Rows = 3;
|
||||||
|
$attributes = $textField->getFormField()->getAttributes();
|
||||||
|
$this->assertFalse(isset($attributes['maxLength']));
|
||||||
|
$this->assertEquals(10, $attributes['data-rule-minlength']);
|
||||||
|
$this->assertEquals(20, $attributes['data-rule-maxlength']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user