diff --git a/spoon/form/input.php b/spoon/form/input.php index d6e60ed..1b4f963 100644 --- a/spoon/form/input.php +++ b/spoon/form/input.php @@ -129,4 +129,16 @@ public function setError($error) $this->errors = (string) $error; return $this; } + + /** + * Marks a field as required + * + * @return self + */ + public function makeRequired() + { + $this->setAttribute('required', 'required'); + + return $this; + } } diff --git a/spoon/form/text.php b/spoon/form/text.php index 8af2557..1b87db6 100644 --- a/spoon/form/text.php +++ b/spoon/form/text.php @@ -797,4 +797,18 @@ public function parse($template = null) return $output; } + + /** + * Sets the html type attribute. This makes it easier to mark a field as + * email/number/... + * + * @param string + * @return self + */ + public function setType($type) + { + $this->setAttribute('type', $type); + + return $this; + } } diff --git a/spoon/tests/form/SpoonFormTextTest.php b/spoon/tests/form/SpoonFormTextTest.php index 1fc4850..47430bb 100644 --- a/spoon/tests/form/SpoonFormTextTest.php +++ b/spoon/tests/form/SpoonFormTextTest.php @@ -292,4 +292,43 @@ public function testGetValue() $_POST['name'] = array('foo', 'bar'); $this->assertEquals('Array', $this->txtName->getValue()); } + + public function testType() + { + $this->txtName->setType('email'); + $this->assertEquals( + 'email', + $this->txtName->getAttributes()['type'] + ); + } + + public function testRequired() + { + $this->txtName->makeRequired(); + $this->assertEquals( + 'required', + $this->txtName->getAttributes()['required'] + ); + } + + public function testChainingMethods() + { + $this->txtName + ->setType('email') + ->makeRequired() + ->setError('test'); + + $this->assertEquals( + 'email', + $this->txtName->getAttributes()['type'] + ); + $this->assertEquals( + 'required', + $this->txtName->getAttributes()['required'] + ); + $this->assertEquals( + 'test', + $this->txtName->getErrors() + ); + } }