Skip to content

Commit

Permalink
Adding ability to override templates for a single input field
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 23, 2014
1 parent f8695d2 commit 2a35a74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
26 changes: 19 additions & 7 deletions src/View/Helper/FormHelper.php
Expand Up @@ -859,10 +859,15 @@ public function input($fieldName, $options = array()) {
'label' => null,
'error' => null,
'selected' => null,
'options' => null
'options' => null,
'templates' => []
];
$options = $this->_parseOptions($fieldName, $options);
$options += ['id' => $this->_domId($fieldName)];

$originalTemplates = $this->templates();
$this->templates($options['templates']);
unset($options['templates']);
$label = $this->_getLabel($fieldName, $options);
if ($options['type'] !== 'radio') {
unset($options['label']);
Expand All @@ -877,13 +882,19 @@ public function input($fieldName, $options = array()) {
}

$input = $this->{$options['type']}($fieldName, $options);
$result = $this->formatTemplate('formGroup', compact('input', 'label'));

if ($options['type'] !== 'hidden') {
$result = $this->formatTemplate($template, [
'content' => $result,
'error' => $error,
'required' => null,
'type' => $options['type'],
]);
}

return $this->formatTemplate($template, [
'content' => $this->formatTemplate('formGroup', compact('input', 'label')),
'error' => $error,
'required' => null,
'type' => $options['type'],
]);
$this->templates($originalTemplates);
return $result;
}

/**
Expand Down Expand Up @@ -918,6 +929,7 @@ protected function _inputType($fieldName, $options) {
$internalType = $context->type($fieldName);
$map = $this->settings['typeMap'];
$type = isset($map[$internalType]) ? $map[$internalType] : 'text';
$fieldName = array_slice(explode('.', $fieldName), -1)[0];

switch (true) {
case isset($options['checked']) :
Expand Down
58 changes: 33 additions & 25 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -71,7 +71,8 @@ class ContactsTable extends Table {
'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
'_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
);

/**
Expand Down Expand Up @@ -113,15 +114,6 @@ class ContactsTable extends Table {
'boolean_field' => array('rule' => 'boolean')
);

/**
* schema method
*
* @return void
*/
public function setSchema($schema) {
$this->_schema = $schema;
}

/**
* hasAndBelongsToMany property
*
Expand All @@ -142,6 +134,16 @@ public function setSchema($schema) {
public $belongsTo = array(
'User' => array('className' => 'UserForm'
));

/**
* Initializes the schema and validation rules.
*
* @return void
*/
public function initialize(array $config) {
$this->schema($this->_schema);
}

}

/**
Expand Down Expand Up @@ -2424,7 +2426,7 @@ public function testInput() {
'label' => array('for'),
'Balance',
'/label',
'input' => array('name', 'type' => 'number', 'step'),
'input' => array('name', 'type' => 'number', 'id', 'step'),
'/div',
);
$this->assertTags($result, $expected);
Expand All @@ -2439,10 +2441,21 @@ public function testInput() {
'/div',
);
$this->assertTags($result, $expected);
}

/**
* Tests the input method and passing custom options
*
* @return void
*/
public function testInputCustomization() {
TableRegistry::get('Contacts', [
'className' => __NAMESPACE__ . '\ContactsTable'
]);
$this->Form->create([], ['context' => ['table' => 'Contacts']]);
$result = $this->Form->input('Contact.email', array('id' => 'custom'));
$expected = array(
'div' => array('class' => 'input email'),
'div' => array('class' => 'email'),
'label' => array('for' => 'custom'),
'Email',
'/label',
Expand All @@ -2454,36 +2467,31 @@ public function testInput() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
$result = $this->Form->input('Contact.email', array(
'templates' => ['groupContainer' => '<div>{{content}}</div>']
));
$expected = array(
'<div',
'label' => array('for' => 'ContactEmail'),
'label' => array('for' => 'contact-email'),
'Email',
'/label',
array('input' => array(
'type' => 'email', 'name' => 'Contact[email]',
'id' => 'ContactEmail', 'maxlength' => 255
'id' => 'contact-email', 'maxlength' => 255
)),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->hidden('Contact.idontexist');
$expected = array('input' => array(
'type' => 'hidden', 'name' => 'Contact[idontexist]',
'id' => 'ContactIdontexist'
));
$this->assertTags($result, $expected);

$result = $this->Form->input('Contact.email', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
'label' => array('for' => 'ContactEmail'),
'div' => array('class' => 'text'),
'label' => array('for' => 'contact-email'),
'Email',
'/label',
array('input' => array(
'type' => 'text', 'name' => 'Contact[email]',
'id' => 'ContactEmail'
'id' => 'contact-email', 'maxlength' => '255'
)),
'/div'
);
Expand Down

0 comments on commit 2a35a74

Please sign in to comment.