Skip to content

Instantly share code, notes, and snippets.

@garak
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garak/3ec370c8b19dd9d86f08 to your computer and use it in GitHub Desktop.
Save garak/3ec370c8b19dd9d86f08 to your computer and use it in GitHub Desktop.
{% extends "::layout.html.twig" %}
{% block body %}
<h3>Contact</h3>
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock%}
<?php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints;
class ContactType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('email', 'email')
->add('message', 'textarea')
->add('privacy', 'checkbox', array('label' => 'I authorize.'))
->add('button', 'submit', array('label' => 'Send'))
;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$constraints = new Constraints\Collection(array('fields' => array(
'name' => array(new Constraints\NotBlank(), new Constraints\Length(array('max' => 100))),
'email' => array(new Constraints\NotBlank(), new Constraints\Email()),
'message' => array(new Constraints\NotBlank(), new Constraints\Length(array('max' => 900))),
'privacy' => new Constraints\NotNull(),
)));
$resolver
->setDefaults(array('constraints' => $constraints, 'attr' => array('novalidate' => true)))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'info';
}
}
<?php
# use...
class DemoController extends Controller
{
public function contactAction(Request $request)
{
$form = $this->createForm(new ContactType());
if ($form->handleRequest($request)->isValid()) {
// ...
}
return array('form' => $form->createView());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment