08.03.2015 16:22:00 • Categories: Zend Framework 2, Twig • Tags: Twig, Zend Framework 2, Forms
ZF2: Zend Form in Twig Templates ausgeben
Einige Notizen zur Umsetzung einer Zend Form mit Ausgabe in einem Twig Template.
Form Klasse:
<?phpForm Validieren: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 ) ) )); }}
<?php namespace Application\Model\Form;From in Controller an View übergeben: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; }}
$formValidator = new Contact();
$form = new ContactForm();
$form->setInputFilter($formValidator->getInputFilter());
$form->setAttribute('action', $this->url()->fromRoute('kontakt'));
$view->setVariables(
array(
'form' => $form,
)
);</pre>
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;
}
}
}</pre>
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 }}