Symfony2 - Kontakt FormBuilder. Variable form existiert nicht im twig-template. Eine Website-Seite

Sitze ich drei Stunden unter symfony2 FormBuilder, wenn ich versuche, ein einfaches Kontaktformular auf meiner OneSitePage website. Ich werde bemerken, dass ich meistens frontend, aber ich brauche das senden von E-Mails per Swiftmailer über symfony2. Bitte nicht Fragen, warum ich bin mit symfony:)

PROBLEM: ich habe Problem mit render-Formular auf meiner homePage, da Symfony sagt, wie in Thema:

"Variable "form" existiert nicht in YodaHomeBundle::layout.html-Code.Zweig..."
und es Punkt für die Linie, wo ich bin useing Zweig-Formular(unten angehängt im ZWEIG Abschnitt)

Ok, das war die Einleitung. Unten präsentiere ich die PHP-Klasse von controller und contacttype ab Klasse, auch unten habe ich angeschlossen hatte, layout.html-Code.twig Datei.

Als erstes kommt der controller, wo ich zwei Aktionen, index und Kontakt.

namespace Yoda\HomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Yoda\UserBundle\Entity\User;
use Yoda\HomeBundle\Form\ContactType;
use Symfony\Component\Form\FormInterface;


class HomeController extends Controller{

    /**
      * @Route("/home", name="homePage")
      * @Template()
      *
      */
    public function indexAction(){

        return $this->render('YodaHomeBundle::layout.html.twig');

    }

    public function contactAction(Request $request)
    {

        $form = $this->createForm(new ContactType());

        $adress = '[email protected]';

        if($request->isMethod('POST'))
        {
            $form->submit($request->request->get($form->getName()));

            if($form->isValid())
            {
                $message = \Swift_Message::newInstance()
                    ->setSubject($form->get('subject')->getData())
                    ->setFrom($form->get('email')->getData())
                    ->setTo($adress)
                    ->setBody(
                        $this->renderView('@YodaHome/mail/contact.html.twig',
                            array(
                                'ip'        =>  $request->getClientIp(),
                                'name'      =>  $form->get('name')->getData(),
                                'message'   =>  $form->get('message')->getData()
                            ))
                    );

                $this->get('mailer')->send($message);

                $request->getSession()->getFlashBag()->add('Success, your mail has been send! Thank you, I will back to you, as soon as it\'s possible!');

                return $this->redirect($this->generateUrl('homePage'));

            }
        }

        return array(
            'form' => $form->createView()
        );

    }

}

nun-generator, Einfache generator, der verwendet wird, auf viele tuts.

class ContactType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text', array(
            'attr' => array(
                'placeholder'   => 'What\'s your name?',
                'length'        => '.{2,}'
            )
        ))
        ->add('email', 'email', array(
            'attr' => array(
                'placeholder'   => 'So I can write back to you'
            )
        ))
        ->add('subject', 'text', array(
            'attr' => array(
                'placeholder'   => 'Subject of your message',
                'pattern'       => '.{5,}'
            )
        ))
        ->add('message', 'text', array(
            'attr' => array(
                'cols'          => '90',
                'row'           => '10',
                'placeholder'   => 'And ad your message to me...'
            )
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => array(
                new NotBlank(array('message' => 'You forgot about the Name.')),
                new Length(array('min' => 2))
            ),
            'email' => array(
                new NotBlank(array('message' => 'Email should not be blank.')),
                new Email(array('message' => 'Invalid email address.'))
            ),
            'subject' => array(
                new NotBlank(array('message' => 'Subject should not be blank.')),
                new Length(array('min' => 3))
            ),
            'message' => array(
                new NotBlank(array('message' => 'Message should not be blank.')),
                new Length(array('min' => 5))
            )
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }

    public function getName()
    {
        return 'homePage';
    }

Und für den letzten Platz routing und ZWEIG:

mail_create:
    path:     /homePage
    defaults: { _controller: "YodaHomeBundle:Home:contact" }
    requirements: { _method: post }

[...]
    <form action="{{ path('mail_create') }}" method="post">
                    {{ form_start(form) }}
                    {{ form_widget(form) }}
                    {{ form_end(form) }}
    </form>
[...]

Bitte für die Unterstützung, überall sind Lösungen für verschiedene Routen für den Kontakt, und ich habe alles auf eine Seite. Alle Vorschläge sind willkommen, bitte um Kommentare!

Uland

Schreibe einen Kommentar