Title: Modifying Symfony 2 forms behavior
Tags: jquery;symfony;symfony-forms
Question: I am using Symfony 2.
The way my forms are working is like the following :
Forms are submitted in Ajax (JQuery)
If there are errors in my form, I receive an XML response with all error messages
<errors&gt;
<error id="name"&gt;This field cannot be blank</error&gt;
<error id="email"&gt;This email address is not valid</error&gt;
<error id="birthday"&gt;Birthday cannot be in the future</error&gt;
</errors&gt;
If there is no error in my form, I receive an XML response with redirect URL
<redirect url="/confirm"&gt;</redirect&gt;
My question is : how can I change "forever" the behavior of forms in Symfony 2 so that I could use a controller like the following :
public function registerAction(Request $request)
$member = new Member();
$form = $this->createFormBuilder($member)
->add('name', 'text')
->add('email', 'email')
->add('birthday', 'date')
->getForm();
if($request->getMethod() == 'POST') {
$form->bindRequest($request);
if($form->isValid()) {
// returns XML response with redirect URL
else {
// returns XML response with error messages
// returns HTML form
Thanks for your help,
Regards,


Here is another answer: Consider encapsulating this logic in a "form handler" service, similar to what is done in FOSUserBundle:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Form/Handler/RegistrationFormHandler.php

Here is another answer: A form handler is how I do it as well. Validate the form in the formHandler and create your json or xml response in the controller depending on the response from the formHandler.
<?php
namespace Application\CrmBundle\Form\Handler;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Application\CrmBundle\Entity\Note;
use Application\CrmBundle\Entity\NoteManager;
class NoteFormHandler
protected $form;
protected $request;
protected $noteManager;
public function __construct(Form $form, Request $request, NoteManager $noteManager)
$this-&gt;form = $form;
$this-&gt;request = $request;
$this-&gt;noteManager = $noteManager;
public function process(Note $note = null)
if (null === $note) {
$note = $this-&gt;noteManager-&gt;create();
}
$this-&gt;form-&gt;setData($note);
if ('POST' == $this-&gt;request-&gt;getMethod()) {
$this-&gt;form-&gt;bindRequest($this-&gt;request);
if ($this-&gt;form-&gt;isValid()) {
$this-&gt;onSuccess($note);
return true;
} else {
$response = array();
foreach ($this-&gt;form-&gt;getChildren() as $field) {
$errors = $field-&gt;getErrors();
if ($errors) {
$response[$field-&gt;getName()] = strtr($errors[0]-&gt;getMessageTemplate(), $errors[0]-&gt;getMessageParameters());
}
}
return $response;
}
}
return false;
protected function onSuccess(Note $note)
$this-&gt;noteManager-&gt;update($note);
This only returns 1 error message per field but it does the trick for me.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:01/20/12', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<jquery><symfony><symfony-forms>', 'Title': 'Modifying Symfony 2 forms behavior', 'ViewCount': 777, 'id': 8940571, 'user_AboutMe': None, 'user_AccountId': 1004399.0, 'user_DisplayName': 'user1018527', 'user_Reputation': 421.0, 'user_Views': 7.0, 'user_id': 1018527.0}}