Skip to content

Instantly share code, notes, and snippets.

@SebCorbin
Created December 7, 2016 15:14
Show Gist options
  • Save SebCorbin/95feefd34fc5c447158b31d8bbf00ac3 to your computer and use it in GitHub Desktop.
Save SebCorbin/95feefd34fc5c447158b31d8bbf00ac3 to your computer and use it in GitHub Desktop.
PoneyBundle - An example of contact form for poneys
{# PoneyBundle/Resources/views/Form/contact.html.twig #}
{{ form_start(form) }}
{{ form_errors(form) }}
<fieldset>
<legend>Informations personnelles</legend>
{{ form_row(form.name) }}
{{ form_row(form.mail) }}
</fieldset>
<fieldset>
<legend>Message</legend>
{{ form_row(form.reason_text) }}
</fieldset>
{{ form(form) }}
{{ form_end(form) }}
<?php
// PoneyBundle/Controller/FormController.php
namespace PoneyBundle\Controller;
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints as Assert;
class FormController extends Controller
{
public function contactAction(Request $request)
{
$form = $this->createFormBuilder()
->add('name', TextType::class, [
'label' => "Nom",
'attr' => ['placeholder' => "Jean Dupont"],
'required' => true,
])
->add('mail', EmailType::class, [
'label' => "Adresse e-mail",
'attr' => ['placeholder' => "[email protected]"],
'required' => true,
'constraints' => [
new Assert\Email(),
new Assert\Length(['max' => 63]),
],
])
->add('reason_text', TextareaType::class, [
'label' => "Message",
'required' => true,
'attr' => [
'placeholder' => "",
'rows' => 10,
],
'constraints' => [
new Assert\NotBlank(),
],
])
->add('recaptcha', EWZRecaptchaType::class, [
'mapped' => false,
'constraints' => [
new IsTrue(),
],
])
->add('submit', SubmitType::class, [
'label' => "Envoyer",
])
->getForm()
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$values = $form->getData();
// @TODO faire quelque chose de ces données
}
return $this->render('PoneyBundle:Form:contact.html.twig', [
'form' => $form->createView(),
]);
}
}
<?php
// PoneyBundle/PoneyBundle.php
namespace PoneyBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PoneyBundle extends Bundle
{
}
# PoneyBundle/Resources/config/routing.yml
contact:
path: contact
defaults:
_controller: PoneyBundle:Form:contact
methods: [GET, POST]
options:
drupal:
title: Contact
access arguments: ['use contact form']
type: MENU_NORMAL_ITEM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment