Example #1
0
File: Radio.php Project: JBZoo/Html
 /**
  * Generates an HTML radio list.
  *
  * @param array $options
  * @param string $name
  * @param array $selected
  * @param array $attrs
  * @param bool|false $tpl
  * @return string
  */
 public function render(array $options, $name, $selected = array(), array $attrs = array(), $tpl = false)
 {
     if (is_array($selected)) {
         $last = Arr::last($selected);
         $selectedVal = Arr::in($last, $selected) ? $last : null;
         list($options, $selected) = $this->_checkSelected($options, $selectedVal);
     }
     if (is_string($selected)) {
         list($options, $selected) = $this->_checkSelected($options, $selected);
     }
     return parent::render($options, $name, $selected, $attrs, $tpl);
 }
Example #2
0
 /**
  * Test radio list output.
  *
  * return void
  */
 public function testRadio()
 {
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     $html = $this->radio->render(array('val-1', 'val-2', 'val-3'), 'test', array(), array(), true);
     $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-0'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 0, 'class' => 'jb-val-0')), ' val-1', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-1'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 1, 'class' => 'jb-val-1')), ' val-2', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-2'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 2, 'class' => 'jb-val-2')), ' val-3', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //  Test data and checked single option by array.
     $html = $this->radio->render(array('<p>tag</p>' => 'Tag', ' ' => 'Empty', '"Test label 3"' => 'Custom', 'значение' => 'Translate cyrillic value', 'common' => 'Common label'), 'test', array('common'), array(), true);
     $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-lt-p-gt-tag-lt-p-gt'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '&lt;p&gt;tag&lt;/p&gt;', 'class' => 'jb-val-lt-p-gt-tag-lt-p-gt')), ' Tag', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => ' ', 'class' => 'jb-val-')), ' Empty', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-quot-test-label-3-quot'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '&quot;Test label 3&quot;', 'class' => 'jb-val-quot-test-label-3-quot')), ' Custom', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-znachenie'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'значение', 'class' => 'jb-val-znachenie')), ' Translate cyrillic value', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common', 'checked' => 'checked')), ' Common label', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //  Test checked single option by string.
     $html = $this->radio->render(array('"Test label 3"' => 'Custom', 'значение' => 'Translate cyrillic value', 'common' => 'Common label'), 'test', 'common', array(), true);
     $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-quot-test-label-3-quot'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => '&quot;Test label 3&quot;', 'class' => 'jb-val-quot-test-label-3-quot')), ' Custom', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-znachenie'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'значение', 'class' => 'jb-val-znachenie')), ' Translate cyrillic value', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common', 'checked' => 'checked')), ' Common label', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     $html = $this->radio->render(array('test-7' => 'Test label 7', 'test-8' => 'Test label 8'), 'test', array('common', 'test-8', 'test-2', 'test-4'));
     $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'no-exits', 'class' => 'jb-val-no-exits', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-no-exits')), 'No exits', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7')), 'Test label 7', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8')), 'Test label 8', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     $html = $this->radio->render(array('test-7' => 'Test label 7', 'test-8' => 'Test label 8'), 'test', 'test');
     $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'no-exits', 'class' => 'jb-val-no-exits', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-no-exits')), 'No exits', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7')), 'Test label 7', '/label', array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8')), 'Test label 8', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //  Test checked options by array.
     $html = $this->radio->render(array('test-1' => 'Test label 1', 'test-2' => 'Test label 2', 'test-3' => 'Test label 3', 'test-4' => 'Test label 4', 'test-6' => 'Test label 6', 'test-7' => 'Test label 7', 'test-8' => 'Test label 8', 'common' => 'Common label'), 'test', array('common', 'test-8', 'test-2', 'test-4'), array(), true);
     $expected = array(array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-1'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-1', 'class' => 'jb-val-test-1')), ' Test label 1', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-2'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-2', 'class' => 'jb-val-test-2')), ' Test label 2', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-3'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-3', 'class' => 'jb-val-test-3')), ' Test label 3', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-4'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-4', 'class' => 'jb-val-test-4', 'checked' => 'checked')), ' Test label 4', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-6'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-6', 'class' => 'jb-val-test-6')), ' Test label 6', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-7'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-7', 'class' => 'jb-val-test-7')), ' Test label 7', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-8'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-8', 'class' => 'jb-val-test-8')), ' Test label 8', '/label', array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-common'), 'input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'common', 'class' => 'jb-val-common')), ' Common label', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     //  Test setup callable template.
     $options = array('test-1' => 'Test label 1');
     $html = $this->radio->render($options, 'test', 0, array(), function ($list, $name, $value, $id, $text, $attrs) {
         $alias = Str::slug($value, true);
         $inpClass = 'jb-val-' . $alias;
         $input = $list->input($name, $value, $id, $inpClass, $attrs);
         $text = '<span class="label-title">' . $text . '</span>';
         $label = $list->label($id, $alias, $text);
         return implode(PHP_EOL, array($input, $label));
     });
     $expected = array(array('input' => array('id' => 'preg:/radio-[0-9]+/', 'name' => 'test', 'type' => 'radio', 'value' => 'test-1', 'class' => 'jb-val-test-1', 'checked' => 'checked')), array('label' => array('for' => 'preg:/radio-[0-9]+/', 'class' => 'jb-radio-lbl jb-label-test-1')), 'span' => array('class' => 'label-title'), 'Test label 1', '/span', '/label');
     isHtml($expected, $html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     $html = $this->radio->render($options, 'test', 0, array(), 'no-exits');
     isEmpty($html);
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     $inputReload = Html::_('checkbox', 'Custom\\Html');
     $html = $inputReload->render($options, 'test');
     $expected = array('div' => array('class' => 'jb-input jb-checkbox'), 'label' => array('for' => 'preg:/checkbox-[0-9]+/', 'class' => 'jb-checkbox-lbl jb-label-test-1'), array('input' => array('id' => 'preg:/checkbox-[0-9]+/', 'name' => 'test', 'type' => 'checkbox', 'value' => 'test-1', 'class' => 'jb-val-test-1')), 'span' => array('class' => 'label-title'), 'Test label 1', '/span', '/label', '/div');
     isHtml($expected, $html);
 }
Example #3
0
 /**
  * Test radio list output.
  *
  * return void
  */
 public function testCheckBoxChecked()
 {
     $html = $this->checkbox->render(array('val-1', 'val-2', 'val-3'), 'test', array('0', '2'), array(), true);
     $expected = array(array('label' => array('for' => 'preg:/checkbox-[0-9]+/', 'class' => 'jb-checkbox-lbl jb-label-0'), 'input' => array('id' => 'preg:/checkbox-[0-9]+/', 'name' => 'test', 'type' => 'checkbox', 'value' => 0, 'class' => 'jb-val-0', 'checked' => 'checked')), ' val-1', '/label', array('label' => array('for' => 'preg:/checkbox-[0-9]+/', 'class' => 'jb-checkbox-lbl jb-label-1'), 'input' => array('id' => 'preg:/checkbox-[0-9]+/', 'name' => 'test', 'type' => 'checkbox', 'value' => 1, 'class' => 'jb-val-1')), ' val-2', '/label', array('label' => array('for' => 'preg:/checkbox-[0-9]+/', 'class' => 'jb-checkbox-lbl jb-label-2'), 'input' => array('id' => 'preg:/checkbox-[0-9]+/', 'name' => 'test', 'type' => 'checkbox', 'value' => 2, 'class' => 'jb-val-2', 'checked' => 'checked')), ' val-3', '/label');
     isHtml($expected, $html);
 }