Adding a magic __call method to handle html5 inputs and simplify input generation logic.

This commit is contained in:
mark_story 2010-08-17 00:06:23 -04:00
parent e2fee17173
commit e79df23491
2 changed files with 60 additions and 0 deletions

View file

@ -1148,6 +1148,32 @@ class FormHelper extends AppHelper {
); );
} }
/**
* 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. * Creates a textarea widget.
* *

View file

@ -6364,4 +6364,38 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $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();
}
} }