Gc\View\Helper\FormCheckbox::render PHP Method

render() public method

Render a form element from the provided $element
public render ( Zend\Form\ElementInterface $element ) : string
$element Zend\Form\ElementInterface Zend Form Element
return string
    public function render(ElementInterface $element)
    {
        if (!$element instanceof CheckboxElement) {
            throw new Exception\InvalidArgumentException(sprintf('%s requires that the element is of type Zend\\Form\\Element\\Checkbox', __METHOD__));
        }
        $name = $element->getName();
        if (empty($name) && $name !== 0) {
            throw new Exception\DomainException(sprintf('%s requires that the element has an assigned name; none discovered', __METHOD__));
        }
        $attributes = $element->getAttributes();
        $attributes['name'] = $name;
        $attributes['type'] = $this->getInputType();
        $attributes['value'] = $element->getCheckedValue();
        $closingBracket = $this->getInlineClosingBracket();
        if ($element->isChecked()) {
            $attributes['checked'] = 'checked';
        }
        if ($element->getAttribute('class') != 'input-checkbox') {
            $rendered = sprintf('<input %s%s', $this->createAttributesString($attributes), $closingBracket);
        } else {
            if ($element->getAttribute('id') == '') {
                $element->setAttribute('id', 'checkbox-' . uniqid());
            }
            unset($attributes['class']);
            $rendered = sprintf('<span class="input-checkbox"><input %s%s<label for="%s"></label></span>', $this->createAttributesString($attributes), $closingBracket, $element->getAttribute('id'));
        }
        return $rendered;
    }

Usage Example

Beispiel #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testRenderWithClass()
 {
     $this->element->setValue('checked');
     $this->element->setAttribute('class', 'input-checkbox');
     $markup = $this->object->render($this->element);
     $this->assertContains('checked="checked"', $markup);
     $this->assertContains('class="input-checkbox"', $markup);
 }
FormCheckbox