/**
  * Renders a label element for a given radio button.
  *
  * In the future this might be refactored into a separate widget as other
  * input types (multi-checkboxes) will also need labels generated.
  *
  * @param array $radio The input properties.
  * @param false|string|array $label The properties for a label.
  * @param string $input The input widget.
  * @param \Cake\View\Form\ContextInterface $context The form context.
  * @param bool $escape Whether or not to HTML escape the label.
  * @return string Generated label.
  */
 protected function _renderLabel($radio, $label, $input, $context, $escape)
 {
     if ($this->_inline) {
         $label = ['text' => $radio['text'], 'class' => 'radio-inline'];
     }
     return parent::_renderLabel($radio, $label, $input, $context, $escape);
 }
Example #2
0
 /**
  * Render a set of radio buttons.
  *
  * Data supports the following keys:
  *
  * - `name` - Set the input name.
  * - `inline` - The alignement to use.
  * - `options` - An array of options. See below for more information.
  * - `disabled` - Either true or an array of inputs to disable.
  *    When true, the select element will be disabled.
  * - `val` - A string of the option to mark as selected.
  * - `label` - Either false to disable label generation, or
  *   an array of attributes for all labels.
  * - `required` - Set to true to add the required attribute
  *   on all generated radios.
  * - `idPrefix` Prefix for generated ID attributes.
  *
  * @param array $data The data to build radio buttons with.
  * @param \Cake\View\Form\ContextInterface $context The current form context.
  * @return string
  */
 public function render(array $data, ContextInterface $context)
 {
     $data += ['inline' => false];
     if ($data['inline']) {
         $this->_templates->add(['radioContainer' => '{{content}}']);
     }
     $templates = $this->_templates->get();
     $customize = [];
     if ($templates['nestingLabel'] === '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>') {
         $customize['nestingLabel'] = '<div class="radio">' . $templates['nestingLabel'] . '</div>';
     }
     $this->_templates->add($customize);
     unset($data['inline']);
     return parent::render($data, $context);
 }
Example #3
0
 /**
  * Ensure that template vars work.
  *
  * @return void
  */
 public function testRenderTemplateVars()
 {
     $this->templates->add(['radioWrapper' => '<div class="radio" data-var="{{wrapperVar}}">{{label}}</div>', 'radio' => '<input type="radio" data-i="{{inputVar}}" name="{{name}}" value="{{value}}"{{attrs}}>', 'nestingLabel' => '<label{{attrs}}>{{input}}{{text}} {{labelVar}} {{wrapperVar}}</label>']);
     $label = new NestingLabelWidget($this->templates);
     $radio = new RadioWidget($this->templates, $label);
     $data = ['name' => 'Versions[ver]', 'options' => [['value' => '1x', 'text' => 'one x', 'templateVars' => ['labelVar' => 'l-var', 'inputVar' => 'i-var']], '2' => 'two'], 'templateVars' => ['wrapperVar' => 'wrap-var']];
     $result = $radio->render($data, $this->context);
     $this->assertContains('data-var="wrap-var"><label', $result);
     $this->assertContains('type="radio" data-i="i-var"', $result);
     $this->assertContains('one x l-var wrap-var</label>', $result);
     $this->assertContains('two  wrap-var</label>', $result);
 }
Example #4
0
 /**
  * Ensure that the input + label are composed with
  * a template.
  *
  * @return void
  */
 public function testRenderContainerTemplate()
 {
     $this->templates->add(['radioWrapper' => '<div class="radio">{{input}}{{label}}</div>']);
     $label = new NestingLabelWidget($this->templates);
     $radio = new RadioWidget($this->templates, $label);
     $data = ['name' => 'Versions[ver]', 'options' => [1 => 'one', '1x' => 'one x', '2' => 'two']];
     $result = $radio->render($data, $this->context);
     $this->assertContains('<div class="radio"><input type="radio"', $result);
     $this->assertContains('</label></div>', $result);
 }