From bff2f2370fe88d8ac9cd064ad6dcb57711abd5bb Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 16 May 2016 18:05:05 +1200 Subject: [PATCH] BUG Fix issue with maxlength="0" Fixes #367 --- .../editableformfields/EditableTextField.php | 13 +++++--- tests/EditableFormFieldTest.php | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/code/model/editableformfields/EditableTextField.php b/code/model/editableformfields/EditableTextField.php index 05e2927..8f4f126 100755 --- a/code/model/editableformfields/EditableTextField.php +++ b/code/model/editableformfields/EditableTextField.php @@ -79,7 +79,7 @@ class EditableTextField extends EditableFormField { ->setTemplate('UserFormsTextareaField') ->setRows($this->Rows); } else { - $field = TextField::create($this->Name, $this->EscapedTitle, $this->Default, $this->MaxLength) + $field = TextField::create($this->Name, $this->EscapedTitle, $this->Default) ->setFieldHolderTemplate('UserFormsField_holder') ->setTemplate('UserFormsField'); } @@ -97,12 +97,15 @@ class EditableTextField extends EditableFormField { protected function updateFormField($field) { parent::updateFormField($field); - if($this->MinLength) { - $field->setAttribute('data-rule-minlength', $this->MinLength); + if(is_numeric($this->MinLength) && $this->MinLength > 0) { + $field->setAttribute('data-rule-minlength', intval($this->MinLength)); } - if($this->MaxLength) { - $field->setAttribute('data-rule-maxlength', $this->MaxLength); + if(is_numeric($this->MaxLength) && $this->MaxLength > 0) { + if($field instanceof TextField) { + $field->setMaxLength(intval($this->MaxLength)); + } + $field->setAttribute('data-rule-maxlength', intval($this->MaxLength)); } if($this->Placeholder) { diff --git a/tests/EditableFormFieldTest.php b/tests/EditableFormFieldTest.php index 484492d..eb83d7e 100644 --- a/tests/EditableFormFieldTest.php +++ b/tests/EditableFormFieldTest.php @@ -144,4 +144,35 @@ class EditableFormFieldTest extends FunctionalTest { $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']); + } + + }