Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Last active April 15, 2016 09:29
Show Gist options
  • Save fabiopaiva/4820db170e62ffea66a6 to your computer and use it in GitHub Desktop.
Save fabiopaiva/4820db170e62ffea66a6 to your computer and use it in GitHub Desktop.
ZF2 Custom Form Render
<?php echo $this->form()->openTag($form); ?>
<?php echo $this->renderForm($form); ?>
<input type="submit" class="btn btn-sm btn-success" value="Salvar"/>
<?php echo $this->form()->closeTag($form); ?>
<?php
return array(
'view_helpers' => array(
'invokables' => array(
'render_form' => 'Application\View\Helper\RenderForm'
),
),
);
<?php
namespace Application\View\Helper;
//use Zend\View\Helper\AbstractHelper;
use Zend\Form\View\Helper\AbstractHelper;
use Zend\Form\Form;
class RenderForm extends AbstractHelper {
public function __invoke(Form $form) {
$this->validTagAttributes = array_merge(
$this->validTagAttributes, array(
"ng-show" => true,
"ng-animate" => true,
"class" => true
)
);
$html = '';
if ($form->getMessages()) {
foreach ($form->getElements() as $element) {
foreach ($element->getMessages() as $msg) {
$html .= '<div class="alert alert-danger alert-dismissable">'
. '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
$html .= "<b>[{$element->getLabel()}]</b> " . $msg;
$html .= '</div>';
}
}
}
$html .= $this->render($form);
return $html;
}
public function renderElement(\Zend\Form\Element $element) {
$html = '<div ';
$attributes = (array) $element->getOption('wrapper-attributes');
$attributes['class'] = (isset($attributes['class']) ? $attributes['class'] :'') . ' form-group';
$html .= $this->createAttributesString($attributes);
$html .='>';
$html .= '<label for="' . $element->getName() . '" class="col-sm-2 control-label">'
. $element->getLabel()
. '</label>';
if ($element->getOption('append')) {
$html .= '<div class="col-sm-8">';
} else {
$html .= '<div class="col-sm-10">';
}
$html.= $this->getView()->formElement($element);
$html .= '</div>';
if ($element->getOption('append')) {
$html.= '<div class="col-sm-2">'
. $element->getOption('append')
. '</div>';
}
$html .= '</div>';
return $html;
}
public function render($elements) {
$html = '';
foreach ($elements as $element) {
if ($element instanceof \Zend\Form\Fieldset) {
$html.= '<fieldset>';
if ($element->getLabel() != '') {
$html.= '<legend>' . $element->getLabel() . '</legend>';
}
if ($element->getMessages()) {
foreach ($element->getElements() as $elementfs) {
foreach ($elementfs->getMessages() as $msg) {
$html .= '<div class="alert alert-danger alert-dismissable">'
. '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
$html .= "<b>[{$elementfs->getLabel()}]</b> " . $msg;
$html .= '</div>';
}
}
}
if ($element instanceof \Zend\Form\Element\Collection && $element->shouldCreateTemplate()) {
$html.= "<fieldset>" .
$this->render($element) .
"</fieldset>";
$escapeHtmlAttribHelper = $this->getView()->plugin('escapehtmlattr');
if ($element->getTemplateElement() instanceof \Zend\Form\Fieldset) {
$templateMarkup = $this->render($element->getTemplateElement());
} else {
$templateMarkup = $this->renderElement($element->getTemplateElement());
}
$template = "<fieldset>" .
$templateMarkup
. "<hr/></fieldset>"
;
$html .= sprintf(
'<span class="template" data-template="%s"></span>', $escapeHtmlAttribHelper($template)
);
if (!is_null($element->getOption('add_button'))) {
$buttonRender = $this->getView()->plugin('formButton');
$botao = $element->getOption('add_button');
if (!$botao instanceof \Zend\Form\Element\Button) {
$factory = new \Zend\Form\Factory();
$botao = $factory->createElement($botao);
}
$html .= '<div class="col-sm-2"></div><div class="col-sm-3">';
$html .= $buttonRender($botao);
$html .= '</div>';
}
if (!is_null($element->getOption('remove_button'))) {
$buttonRender = $this->getView()->plugin('formButton');
$botao = $element->getOption('remove_button');
if (!$botao instanceof \Zend\Form\Element\Button) {
$factory = new \Zend\Form\Factory();
$botao = $factory->createElement($botao);
}
$html .= '<div class="col-sm-3">';
$html .= $buttonRender($botao);
$html .= '</div>';
}
} else {
$html.= $this->render($element);
}
$html.= '<hr/></fieldset>';
} else {
$html.= $this->renderElement($element);
}
}
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment