Alfredo Costa
Alfredo Costa

Reputation: 427

Get individual form collection elements ZF2

I need to use collections to make dynamic inputs, but needed to pull out the individual elements to integrate them into a Bootstrap layout. Unfortunately I can't find any documented way of doing this.

Could someone help-me?

Here is my form: http://pastebin.com/JGy7JEJk Fieldset used in form: http://pastebin.com/VBbG1yyb Form HTML: http://pastebin.com/HHaZZKsB Form View Helper: http://pastebin.com/x7B9aPWG

You can see this in action http://protesto21.com.br/cadastro/encargo-vigencia/atualizar/2/ User admin, password p21

Thanks for all and sorry for my english

Upvotes: 0

Views: 443

Answers (1)

Alfredo Costa
Alfredo Costa

Reputation: 427

Problem solved

public function renderCollection($element)
{
    $return = '';

    if (count($element->getMessages()) > 0) {
        $return .= '<div class="form-group has-error">';
    } else {
        $return .= '<div class="form-group">';
    }

    foreach ($element->getIterator() as $field) {
        $label = ($this->isView) ? '<strong>' . $element->getLabel() . '</strong>' : $element->getLabel();

        $return .= '<label class="col-md-3 control-label">' . $label . '</label>';
        $return .= '<div class="col-md-8" style="margin-bottom: 20px;">';

        foreach ($field->getElements() as $inputs) {

            $return .= '<div style="float: left; margin-right: 20px;">';
            $return .= $inputs->getLabel();
            $return .= $this->renderInput($inputs);

            $return .= '<div style="min-width: 265px;">';

            foreach ($inputs->getMessages() as $mensagem) {
                $return .= "<span class='help-block'>" . $mensagem . "</span>";
            }

            $return .= '</div>';
            $return .= '</div>';
        }

        $return .= '</div>';
        $element->setLabel('');
    }

    $return .= '</div>';

    return $return;
}

Upvotes: 1

Related Questions