sonata-project/SonataAdminBundle — pull request #1106 by to0om (2013-01-14 13:34:04+00:00)
Title: Set flash messages in create and edit actions only if it's not an Ajax request

Hey,

I noticed that error flash messages in the `createAction` and the `editAction` in `CRUDController` are set even if the request is an Ajax request. For the "success" flash messages, the behavior is correct, because the response is returned before the flash is set:

public function createAction()
{
// [SNIP]
if ($this->isXmlHttpRequest()) {
return $this->renderJson(array(
'result' => 'ok',
'objectId' => $this->admin->getNormalizedIdentifier($object)
));
}

$this->get('session')->setFlash('sonata_flash_success','flash_create_success');

However, the error flash messages are always set when the form is not valid:

if (!$isFormValid) {
$this->get('session')->setFlash('sonata_flash_error', 'flash_create_error');

So when a form containing errors is submitted via Ajax, the modal form dialog is closed and the parent form is reloaded in the browser, the error flash message appears on top even though it does not have anything to do with the parent form.

I fixed this bug and tested the code.