This is how my code snippet looks like.
// — this is the code in my controller —-
$registrationForm = $this->createFormBuilder()
->add('email')
->add('password', 'repeated', array('type' => 'password', 'invalid_message' => 'Passwords do not match'))
->getForm();
return $this->render('AcmeHelloBundle:Default:index.html.twig', array('form' => $registrationForm->createView()));
// --- This is the twig file code----
<form action="#" method="post" {{ form_enctype(form) }}>
{{ form_errors(form) }}
{{ form_row( form.email, { 'label': 'E-Mail:' } ) }}
{{ form_errors( form.password ) }}
{{ form_row( form.password.first, { 'label': 'Your password:' } ) }}
{{ form_row( form.password.second, { 'label': 'Repeat Password:' } ) }}
{{ form_rest( form ) }}
<input type="submit" value="Register" />
</form>
Can any one suggest why it is not working using form builder?
In Symfony 2, validation is handled by domain object. So you have to pass an Entity (domain object) to your form.
Code in controller :
1) The form builder will map the form fields with the properties of your entity, and hydrate your form field values with your entity property values.
2) The bind will hydrate your form fields values with all the data posted
3 ) To launch validation
4) if the data posted are valid, you have to redirect to an action to inform user that everything is OK, to avoid displaying an alert message from your broswer who ask if your are sure to repost data.
Entity code :
doc for validation : http://symfony.com/doc/current/book/validation.html
NOTE : there is no need to add some validation on password entity property, the repeatedType done it for you