From 9b1a5ea8e9e50dac9c427984c2fa9961478261ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dami=C3=A1n=20Aguilar?= Date: Sun, 1 May 2016 16:56:10 +0200 Subject: [PATCH 1/4] Add conditional option Sometimes, we required from our form a conditional behaviour, like: $builder->text('email')->disabled(($profile->isAdmin()) ? true : false); --- src/AdamWathan/Form/Elements/FormControl.php | 26 +++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/AdamWathan/Form/Elements/FormControl.php b/src/AdamWathan/Form/Elements/FormControl.php index ff233f7..7d0ffcf 100644 --- a/src/AdamWathan/Form/Elements/FormControl.php +++ b/src/AdamWathan/Form/Elements/FormControl.php @@ -14,9 +14,11 @@ protected function setName($name) $this->setAttribute('name', $name); } - public function required() + public function required($conditional = true) { - $this->setAttribute('required', 'required'); + if ($conditional) { + $this->setAttribute('required', 'required'); + } return $this; } @@ -28,24 +30,30 @@ public function optional() return $this; } - public function disable() + public function disable($conditional = true) { - $this->setAttribute('disabled', 'disabled'); + if ($conditional) { + $this->setAttribute('disabled', 'disabled'); + } return $this; } - public function readonly() + public function readonly($conditional = true) { - $this->setAttribute('readonly', 'readonly'); + if ($conditional) { + $this->setAttribute('readonly', 'readonly'); + } return $this; } - public function enable() + public function enable($conditional = true) { - $this->removeAttribute('disabled'); - $this->removeAttribute('readonly'); + if ($conditional) { + $this->removeAttribute('disabled'); + $this->removeAttribute('readonly'); + } return $this; } From 2b3cc89c377441b0ca8404b485314551d6f84a46 Mon Sep 17 00:00:00 2001 From: daguilarm Date: Mon, 16 Jan 2017 13:06:09 +0100 Subject: [PATCH 2/4] Add tests --- tests/InputContractTest.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/InputContractTest.php b/tests/InputContractTest.php index 86fe389..1a3ad07 100644 --- a/tests/InputContractTest.php +++ b/tests/InputContractTest.php @@ -25,6 +25,15 @@ public function testRequired() $this->assertRegExp($this->elementRegExp('required="required"'), $result, $message); } + public function testConditionalRequired() + { + $text = $this->newTestSubjectInstance('email'); + $result = $text->required(false)->render(); + + $message = "required attribute shouldnt be set"; + $this->assertRegExp($this->elementRegExp('required="required"'), $result, $message); + } + public function testAutofocus() { $text = $this->newTestSubjectInstance(''); @@ -77,6 +86,15 @@ public function testDisable() $this->assertRegExp($this->elementRegExp('disabled="disabled"'), $result, $message); } + public function testConditionalDisable() + { + $text = $this->newTestSubjectInstance('email'); + $result = $text->required(false)->render(); + + $message = "disabled attribute shouldnt be set"; + $this->assertRegExp($this->elementRegExp('disabled="disabled"'), $result, $message); + } + public function testReadyOnly() { $text = $this->newTestSubjectInstance('email'); @@ -86,6 +104,15 @@ public function testReadyOnly() $this->assertRegExp($this->elementRegExp('readonly="readonly"'), $result, $message); } + public function testConditionalReadyOnly() + { + $text = $this->newTestSubjectInstance('email'); + $result = $text->required(false)->render(); + + $message = "readonly attribute shouldnt be set"; + $this->assertRegExp($this->elementRegExp('readonly="readonly"'), $result, $message); + } + public function testEnableDisabled() { $pattern = 'disabled="disabled"'; From 003edffd0a65f0f2b11d2f62ffb33d0a39abe0d2 Mon Sep 17 00:00:00 2001 From: daguilarm Date: Mon, 16 Jan 2017 13:13:58 +0100 Subject: [PATCH 3/4] Update tests --- tests/InputContractTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/InputContractTest.php b/tests/InputContractTest.php index 1a3ad07..a38f014 100644 --- a/tests/InputContractTest.php +++ b/tests/InputContractTest.php @@ -31,7 +31,7 @@ public function testConditionalRequired() $result = $text->required(false)->render(); $message = "required attribute shouldnt be set"; - $this->assertRegExp($this->elementRegExp('required="required"'), $result, $message); + $this->assertNotRegExp($this->elementRegExp('required="required"'), $result, $message); } public function testAutofocus() @@ -92,7 +92,7 @@ public function testConditionalDisable() $result = $text->required(false)->render(); $message = "disabled attribute shouldnt be set"; - $this->assertRegExp($this->elementRegExp('disabled="disabled"'), $result, $message); + $this->assertNotRegExp($this->elementRegExp('disabled="disabled"'), $result, $message); } public function testReadyOnly() @@ -110,7 +110,7 @@ public function testConditionalReadyOnly() $result = $text->required(false)->render(); $message = "readonly attribute shouldnt be set"; - $this->assertRegExp($this->elementRegExp('readonly="readonly"'), $result, $message); + $this->assertNotRegExp($this->elementRegExp('readonly="readonly"'), $result, $message); } public function testEnableDisabled() From 5699ef17a40cea7b62b75b5794edea292ec4e5ed Mon Sep 17 00:00:00 2001 From: daguilarm Date: Mon, 16 Jan 2017 15:41:43 +0100 Subject: [PATCH 4/4] Fixed problem with multiple attributes --- src/AdamWathan/Form/Elements/Element.php | 9 +++++++ src/AdamWathan/Form/Elements/FormControl.php | 26 +++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/AdamWathan/Form/Elements/Element.php b/src/AdamWathan/Form/Elements/Element.php index c593be2..93d7a61 100644 --- a/src/AdamWathan/Form/Elements/Element.php +++ b/src/AdamWathan/Form/Elements/Element.php @@ -126,6 +126,15 @@ protected function splitKeysAndValues($array) return [$keys, $values]; } + protected function setBooleanAttribute($attribute, $value) + { + if ($value) { + $this->setAttribute($attribute, $attribute); + } else { + $this->removeAttribute($attribute); + } + } + public function __call($method, $params) { $params = count($params) ? $params : [$method]; diff --git a/src/AdamWathan/Form/Elements/FormControl.php b/src/AdamWathan/Form/Elements/FormControl.php index 7d0ffcf..112677f 100644 --- a/src/AdamWathan/Form/Elements/FormControl.php +++ b/src/AdamWathan/Form/Elements/FormControl.php @@ -16,10 +16,8 @@ protected function setName($name) public function required($conditional = true) { - if ($conditional) { - $this->setAttribute('required', 'required'); - } - + $this->setBooleanAttribute('required', $conditional); + return $this; } @@ -32,28 +30,22 @@ public function optional() public function disable($conditional = true) { - if ($conditional) { - $this->setAttribute('disabled', 'disabled'); - } - + $this->setBooleanAttribute('disabled', $conditional); + return $this; } public function readonly($conditional = true) { - if ($conditional) { - $this->setAttribute('readonly', 'readonly'); - } - + $this->setBooleanAttribute('readonly', $conditional); + return $this; } - public function enable($conditional = true) + public function enable() { - if ($conditional) { - $this->removeAttribute('disabled'); - $this->removeAttribute('readonly'); - } + $this->removeAttribute('disabled'); + $this->removeAttribute('readonly'); return $this; }