mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Adding a magic __call method to handle html5 inputs and simplify input generation logic.
This commit is contained in:
parent
e2fee17173
commit
e79df23491
2 changed files with 60 additions and 0 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -6364,4 +6364,38 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue