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 ff233f7..112677f 100644 --- a/src/AdamWathan/Form/Elements/FormControl.php +++ b/src/AdamWathan/Form/Elements/FormControl.php @@ -14,10 +14,10 @@ protected function setName($name) $this->setAttribute('name', $name); } - public function required() + public function required($conditional = true) { - $this->setAttribute('required', 'required'); - + $this->setBooleanAttribute('required', $conditional); + return $this; } @@ -28,17 +28,17 @@ public function optional() return $this; } - public function disable() + public function disable($conditional = true) { - $this->setAttribute('disabled', 'disabled'); - + $this->setBooleanAttribute('disabled', $conditional); + return $this; } - public function readonly() + public function readonly($conditional = true) { - $this->setAttribute('readonly', 'readonly'); - + $this->setBooleanAttribute('readonly', $conditional); + return $this; } diff --git a/tests/InputContractTest.php b/tests/InputContractTest.php index 86fe389..a38f014 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->assertNotRegExp($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->assertNotRegExp($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->assertNotRegExp($this->elementRegExp('readonly="readonly"'), $result, $message); + } + public function testEnableDisabled() { $pattern = 'disabled="disabled"';