ZF2: Zend Form in Twig Templates ausgeben

Einige Notizen zur Umsetzung einer Zend Form mit Ausgabe in einem Twig Template.

Form Klasse:

<?php

namespace Application\Form;

use Zend\Captcha;
use Zend\Form\Element;
use Zend\Form\Form;

class ContactForm extends Form
{
    public function __construct($name = null)
    {
        parent::__construct('contact');

        $this->setAttribute('method', 'post');
        $this->setAttribute('class', 'zf2');


        $this->add(array(
            'name' => 'name',
            'type' => 'Zend\Form\Element\Text',
            'attributes' => array(
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Name',
                'label_attributes' => array(
                    'class' => 'required',
                ),
            ),
        ));

        $this->add(array(
            'name' => 'email',
            'type' => 'Zend\Form\Element\Email',
            'attributes' => array(
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Email',
                'label_attributes' => array(
                    'class' => 'required',
                ),
            ),
        ));

        $this->add(array(
            'name' => 'comment',
            'type' => 'Zend\Form\Element\Textarea',
            'attributes' => array(
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Kommentar',
                'label_attributes' => array(
                    'class' => 'required',
                ),
            ),
        ));

        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'class' => 'gradient-button',
                'value' => 'Abschicken',
                'id' => 'submit',
            ),
        ));


        $this->add(array(
            'name' => 'csrf',
            'type' => 'Zend\Form\Element\Csrf',
            'options' => array(
                'csrf_options' => array(
                    'timeout' => 600
                )
            )
        ));
    }
}

Form Validieren:

<?php
namespace Application\Model\Form;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Contact implements InputFilterAwareInterface
{
    protected $inputFilter;

    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter)
        {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();


            $inputFilter->add($factory->createInput([
                'name' => 'name',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array (
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                'isEmpty' => 'Bitte geben Sie Ihren Namen ein.',
                            )
                        ),
                        'break_chain_on_failure' => true,
                    ),
                ),
            ]));

            $inputFilter->add($factory->createInput([
                'name' => 'email',
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array (
                        'name' => 'Regex',
                        'options' => array (
                            'pattern' => '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
                            'message' => array (
                                'regexNotMatch' => 'Bitte geben Sie eine gültige E-Mailadresse ein.',
                            )
                        ),
                        'break_chain_on_failure' => true,
                    ),
                    array (
                        'name' => 'EmailAddress',
                        'options' => array(
                            'messages' => array(
                                'emailAddressInvalidFormat' => 'Bitte geben Sie eine gültige E-Mailadresse ein.',
                            )
                        ),
                        'break_chain_on_failure' => true,
                    ),
                    array (
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                'isEmpty' => 'Bitte geben Sie eine E-Mailadresse ein.',
                            )
                        ),
                        'break_chain_on_failure' => true,
                    ),
                ),
            ]));

            $inputFilter->add($factory->createInput([
                'name' => 'comment',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array (
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                'isEmpty' => 'Bitte geben Sie einen Kommentar ein.',
                            )
                        ),
                        'break_chain_on_failure' => true,
                    ),
                ),
            ]));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

From in Controller an View übergeben:

        $formValidator = new Contact();
        $form = new ContactForm();
        $form->setInputFilter($formValidator->getInputFilter());
        $form->setAttribute('action', $this->url()->fromRoute('kontakt'));


        $view->setVariables(
            array(
                'form' => $form,
            )
        );

Form in Controller verarbeiten:

        $request = $this->getRequest();

        if ($request->isPost()) {

                $form->setData($request->getPost());


            if ($form->isValid()) {
                {

                    $view->setVariables(
                        array(
                            'formValid' => TRUE,
                            'formData' => $form->getData()
                        )
                    );

                    // send mail
                    $this->sendMail($form->getData());

                    return $view;
                }
            }
        }

Form in Twig Template ausgeben.

        {{ form().prepare() }}
        {{ form().openTag(form)|raw }}
        {{ formRow().setInputErrorClass('error').render(form.get('name'))|raw }}
        {{ formRow().setInputErrorClass('error').render(form.get('email'))|raw }}
        {{ formRow().setInputErrorClass('error').render(form.get('comment'))|raw }}
        {{ formRow(form.get('submit'))|raw }}
        {{ formRow(form.get('csrf'))|raw }}
        {{ form().closeTag(form)|raw }}