Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

2.3 FormHelper::radio enhanced options #1032

Closed
wants to merge 2 commits into from

3 participants

Clément Hallet Mark Story José Lorenzo Rodríguez
Clément Hallet
  • add 'afterOption' and 'beforeOption' options, in order to generate more content witihn a radio set. For instance, encapsulate the pair 'label' + 'input' within a 'li'.
  • this 2 can uses the options data to generate content, through sprintf format (%1$s will be the key, while %2$s will be the title of the option), for instance adding an 'img' attached to each option.

See unit tests for examples of usage.
I'll update the documentation once these are accepted.

Clément Hallet

might be related to #864

Mark Story
Owner

Why not expose this as one option 'wrap' that defines the wrapping content for a radio button? Using some simple sprintf() formatting should work fine for this kind of use as there are only two elements that would need to be inserted into the wrapping html.

Clément Hallet

One other use case is to add pictures and links, related to each option, to illustrate them or suggest more information.
This one case can be seen in action in the tests.

I would suggest that 'afterOption' and 'beforeOption' allows for more flexibility than one 'wrap' option.

Mark Story
Owner

But only adding one option keeps the API smaller, requires less mental memory to remember the names of all the options, fewer tests, and less documentation.

Mark Story
Owner

For example the test that contains a link/img could be written as:

<?php
$result = $this->Form->radio(
      'Model.field',
      array('A' => 'option A', 'B' => 'option B'),  
      array(
        'wrapInput' => '<img src="/img/:value.png" alt=":title" />:input<a href="/option/:value">learn more about :title</a>',
      )
    );

The above would use String::insert()/strtr as it is easier to read than sprintf when there are many values being inserted. I'm hoping to coalesce many of the existing options to this style for 3.0 as the existing pile of format/append/prepend options have gotten out of control.

José Lorenzo Rodríguez lorenzo closed this
José Lorenzo Rodríguez
Owner

Please reopen targeting the 2.4 branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 16, 2012
  1. Clément Hallet
  2. Clément Hallet

    add the ability to use sprintf format in 'afterOption' and 'beforeOpt…

    challet authored
    …ion' with the values of options
This page is out of date. Refresh to see the latest.
150 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
View
@@ -3553,6 +3553,156 @@ public function testRadio() {
'/fieldset'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->radio(
+ 'Model.field',
+ array('option A', 'option B'),
+ array('beforeOption' => '<hr />')
+ );
+ $expected = array(
+ 'fieldset' => array(),
+ 'legend' => array(),
+ 'Field',
+ '/legend',
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][field]',
+ 'value' => '', 'id' => 'ModelField_'
+ ),
+ array('hr' => array()),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '0', 'id' => 'ModelField0'
+ )),
+ array('label' => array('for' => 'ModelField0')),
+ 'option A',
+ '/label',
+ array('hr' => array()),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '1', 'id' => 'ModelField1'
+ )),
+ array('label' => array('for' => 'ModelField1')),
+ 'option B',
+ '/label',
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->radio(
+ 'Model.field',
+ array('option A', 'option B'),
+ array('afterOption' => '<br />')
+ );
+ $expected = array(
+ 'fieldset' => array(),
+ 'legend' => array(),
+ 'Field',
+ '/legend',
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][field]',
+ 'value' => '', 'id' => 'ModelField_'
+ ),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '0', 'id' => 'ModelField0'
+ )),
+ array('label' => array('for' => 'ModelField0')),
+ 'option A',
+ '/label',
+ array('br' => array()),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '1', 'id' => 'ModelField1'
+ )),
+ array('label' => array('for' => 'ModelField1')),
+ 'option B',
+ '/label',
+ array('br' => array()),
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->radio(
+ 'Model.field',
+ array('option A', 'option B'),
+ array('beforeOption' => '<li>', 'afterOption' => '</li>')
+ );
+ $expected = array(
+ 'fieldset' => array(),
+ 'legend' => array(),
+ 'Field',
+ '/legend',
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][field]',
+ 'value' => '', 'id' => 'ModelField_'
+ ),
+ array('li' => array()),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '0', 'id' => 'ModelField0'
+ )),
+ array('label' => array('for' => 'ModelField0')),
+ 'option A',
+ '/label',
+ '/li',
+ array('li' => array()),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => '1', 'id' => 'ModelField1'
+ )),
+ array('label' => array('for' => 'ModelField1')),
+ 'option B',
+ '/label',
+ '/li',
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->radio(
+ 'Model.field',
+ array('A' => 'option A', 'B' => 'option B'),
+ array(
+ 'beforeOption' => '<img src="/img/%1$s.png" alt="%2$s" />',
+ 'afterOption' => '<a href="/option/%1$s">learn more about %2$s</a>'
+ )
+ );
+ $expected = array(
+ 'fieldset' => array(),
+ 'legend' => array(),
+ 'Field',
+ '/legend',
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Model][field]',
+ 'value' => '', 'id' => 'ModelField_'
+ ),
+ array('img' => array('src' => '/img/A.png', 'alt' => 'option A')),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => 'A', 'id' => 'ModelFieldA'
+ )),
+ array('label' => array('for' => 'ModelFieldA')),
+ 'option A',
+ '/label',
+ array('a' => array('href' => '/option/A')),
+ 'learn more about option A',
+ '/a',
+ array('img' => array('src' => '/img/B.png', 'alt' => 'option B')),
+ array('input' => array(
+ 'type' => 'radio', 'name' => 'data[Model][field]',
+ 'value' => 'B', 'id' => 'ModelFieldB'
+ )),
+ array('label' => array('for' => 'ModelFieldB')),
+ 'option B',
+ '/label',
+ array('a' => array('href' => '/option/B')),
+ 'learn more about option B',
+ '/a',
+ '/fieldset'
+ );
+ $this->assertTags($result, $expected);
+
+
+
}
/**
27 lib/Cake/View/Helper/FormHelper.php
View
@@ -1332,8 +1332,9 @@ public function checkbox($fieldName, $options = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
*/
public function radio($fieldName, $options = array(), $attributes = array()) {
- $attributes = $this->_initInputField($fieldName, $attributes);
+ $attributes = $this->_initInputField($fieldName, $attributes);
+
$showEmpty = $this->_extractOption('empty', $attributes);
if ($showEmpty) {
$showEmpty = ($showEmpty === true) ? __('empty') : $showEmpty;
@@ -1378,7 +1379,19 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
if (isset($attributes['disabled'])) {
$disabled = $attributes['disabled'];
}
-
+
+ $beforeOption = null;
+ if (isset($attributes['beforeOption'])) {
+ $beforeOption = $attributes['beforeOption'];
+ unset($attributes['beforeOption']);
+ }
+
+ $afterOption = null;
+ if (isset($attributes['afterOption'])) {
+ $afterOption = $attributes['afterOption'];
+ unset($attributes['afterOption']);
+ }
+
$out = array();
$hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
@@ -1398,13 +1411,15 @@ public function radio($fieldName, $options = array(), $attributes = array()) {
);
if ($label) {
- $optTitle = $this->Html->useTag('label', $tagName, '', $optTitle);
+ $optLabel = $this->Html->useTag('label', $tagName, '', $optTitle);
+ } else {
+ $optLabel = $optTitle;
}
$allOptions = array_merge($attributes, $optionsHere);
- $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName,
+ $out[] = sprintf($beforeOption, $optValue, $optTitle) . $this->Html->useTag('radio', $attributes['name'], $tagName,
array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')),
- $optTitle
- );
+ $optLabel
+ ) . sprintf($afterOption, $optValue, $optTitle);
}
$hidden = null;
Something went wrong with that request. Please try again.