Skip to content
Permalink
Browse files

Adding a magic __call method to handle html5 inputs and simplify inpu…

…t generation logic.
  • Loading branch information...
markstory committed Aug 17, 2010
1 parent e2fee17 commit e79df234919c27727d750ebb022acdfa04a3dbc0
Showing with 60 additions and 0 deletions.
  1. +26 −0 cake/libs/view/helpers/form.php
  2. +34 −0 cake/tests/cases/libs/view/helpers/form.test.php
@@ -1148,6 +1148,32 @@ public function password($fieldName, $options = array()) {
);
}
/**
* Missing method handler - implements various simple input types. Is used to create inputs
* of various types. e.g. `$this->Form->text();` will create `<input type="text" />` while
* `$this->Form->range();` will create `<input type="range" />`
*
* @param string $method Method name / input type to make.
* @param array $params Parameters for the method call
* @return string Formatted input method.
*/
public function __call($method, $params) {
$options = array();
if (empty($params)) {
throw new Exception(sprintf(__('Missing field name for FormHelper::%'), $method));
}
if (isset($params[1])) {
$options = $params[1];
}
$options['type'] = $method;
$options = $this->_initInputField($params[0], $options);
return sprintf(
$this->Html->tags['input'],
$options['name'],
$this->_parseAttributes($options, array('name'), null, ' ')
);
}
/**
* Creates a textarea widget.
*
@@ -6364,4 +6364,38 @@ function testInputTemplate() {
);
$this->assertTags($result, $expected);
}
/**
* test that some html5 inputs + FormHelper::__call() work
*
* @return void
*/
function testHtml5Inputs() {
$result = $this->Form->email('User.email');
$expected = array(
'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
);
$this->assertTags($result, $expected);
$result = $this->Form->search('User.query');
$expected = array(
'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
);
$this->assertTags($result, $expected);
$result = $this->Form->search('User.query', array('value' => 'test'));
$expected = array(
'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
);
$this->assertTags($result, $expected);
}
/**
*
* @expectedException Exception
* @return void
*/
function testHtml5InputException() {
$this->Form->email();
}
}

0 comments on commit e79df23

Please sign in to comment.
You can’t perform that action at this time.