Skip to content

Commit

Permalink
Merge pull request #35 from forkcms/forms-syntax
Browse files Browse the repository at this point in the history
Forms syntax
  • Loading branch information
WouterSioen committed Apr 13, 2016
2 parents 13c0d3e + 1e1b090 commit 6eb6865
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spoon/form/input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions spoon/form/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
39 changes: 39 additions & 0 deletions spoon/tests/form/SpoonFormTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

0 comments on commit 6eb6865

Please sign in to comment.