Ejemplo n.º 1
0
 /**
  * @dataProvider getMultiElements
  * @group multi
  */
 public function testRendersMultiElementsAsExpected($type, $inputType, $additionalMarkup)
 {
     if ($type === 'radio') {
         $element = new Element\Radio('foo');
         $this->assertEquals('radio', $element->getAttribute('type'));
     } elseif ($type === 'multi_checkbox') {
         $element = new Element\MultiCheckbox('foo');
         $this->assertEquals('multi_checkbox', $element->getAttribute('type'));
     } elseif ($type === 'select') {
         $element = new Element\Select('foo');
         $this->assertEquals('select', $element->getAttribute('type'));
     } else {
         $element = new Element('foo');
     }
     $element->setAttribute('type', $type);
     $element->setValueOptions(array('value1' => 'option', 'value2' => 'label', 'value3' => 'last'));
     $element->setAttribute('value', 'value2');
     $markup = $this->helper->render($element);
     $this->assertEquals(3, substr_count($markup, '<' . $inputType), $markup);
     $this->assertContains($additionalMarkup, $markup);
     if ($type == 'select') {
         $this->assertRegexp('#value="value2"[^>]*?(selected="selected")#', $markup);
     }
 }
Ejemplo n.º 2
0
 public function testGetSelectedAttributeReturnTrue()
 {
     $element = new MultiCheckboxElement('foo');
     $element->setAttribute('selected', 'true');
     $this->assertSame('true', $element->getAttribute('selected'));
 }
Ejemplo n.º 3
0
 /**
  * Render options
  *
  * @param  MultiCheckboxElement $element
  * @param  array                $options
  * @param  array                $selectedOptions
  * @param  array                $attributes
  * @return string
  */
 protected function renderOptions(MultiCheckboxElement $element, array $options, array $selectedOptions, array $attributes)
 {
     $escapeHtmlHelper = $this->getEscapeHtmlHelper();
     $labelHelper = $this->getLabelHelper();
     $labelClose = $labelHelper->closeTag();
     $labelPosition = $this->getLabelPosition();
     $globalLabelAttributes = $element->getLabelAttributes();
     $closingBracket = $this->getInlineClosingBracket();
     if (empty($globalLabelAttributes)) {
         $globalLabelAttributes = $this->labelAttributes;
     }
     $combinedMarkup = array();
     $count = 0;
     foreach ($options as $key => $optionSpec) {
         $count++;
         if ($count > 1 && array_key_exists('id', $attributes)) {
             unset($attributes['id']);
         }
         $value = '';
         $label = '';
         $inputAttributes = $attributes;
         $labelAttributes = $globalLabelAttributes;
         $selected = isset($inputAttributes['selected']) && $inputAttributes['type'] != 'radio' && $inputAttributes['selected'] != false ? true : false;
         $disabled = isset($inputAttributes['disabled']) && $inputAttributes['disabled'] != false ? true : false;
         if (is_scalar($optionSpec)) {
             $optionSpec = array('label' => $optionSpec, 'value' => $key);
         }
         if (isset($optionSpec['value'])) {
             $value = $optionSpec['value'];
         }
         if (isset($optionSpec['label'])) {
             $label = $optionSpec['label'];
         }
         if (isset($optionSpec['selected'])) {
             $selected = $optionSpec['selected'];
         }
         if (isset($optionSpec['disabled'])) {
             $disabled = $optionSpec['disabled'];
         }
         if (isset($optionSpec['label_attributes'])) {
             $labelAttributes = isset($labelAttributes) ? array_merge($labelAttributes, $optionSpec['label_attributes']) : $optionSpec['label_attributes'];
         }
         if (isset($optionSpec['attributes'])) {
             $inputAttributes = array_merge($inputAttributes, $optionSpec['attributes']);
         }
         if (in_array($value, $selectedOptions)) {
             $selected = true;
         }
         $inputAttributes['value'] = $value;
         $inputAttributes['checked'] = $selected;
         $inputAttributes['disabled'] = $disabled;
         $input = sprintf('<input %s%s', $this->createAttributesString($inputAttributes), $closingBracket);
         if (null !== ($translator = $this->getTranslator())) {
             $label = $translator->translate($label, $this->getTranslatorTextDomain());
         }
         if (empty($labelAttributes)) {
             $labelAttributes = array();
         }
         $mode = $element->getOption('radio_mode');
         $mode = $mode ? $mode : 'inline';
         $labelAttributes['class'] = $element->getAttribute('type') . ' ' . $mode . (empty($labelAttributes['class']) ? '' : ' ' . $labelAttributes['class']);
         $label = $escapeHtmlHelper($label);
         $labelOpen = $labelHelper->openTag($labelAttributes);
         $template = $labelOpen . '%s%s' . $labelClose;
         switch ($labelPosition) {
             case self::LABEL_PREPEND:
                 $markup = sprintf($template, $label, $input);
                 break;
             case self::LABEL_APPEND:
             default:
                 $markup = sprintf($template, $input, $label);
                 break;
         }
         $combinedMarkup[] = $markup;
     }
     return implode($this->getSeparator(), $combinedMarkup);
 }