Last active
August 29, 2015 14:02
-
-
Save garak/3ec370c8b19dd9d86f08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "::layout.html.twig" %} | |
{% block body %} | |
<h3>Contact</h3> | |
{{ form_start(form) }} | |
{{ form_widget(form) }} | |
{{ form_end(form) }} | |
{% endblock%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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