Search code examples
phpsymfonyadminsonata-adminflash-message

How to set flash message in sonata admin Admin Controller


im looking for a way to set flash message in admin controller of sonata admin bundle, they allow to set flash messages in CRUDController as

$this->get('session')->setFlash('sonata_flash_error', 'flash_batch_merge_error');

but not in the Admin Controller,

this is my admin contrller

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;

class ConfigAdmin extends Admin
{

protected function configureFormFields(FormMapper $formMapper)
{   

    $formMapper
        ->with('System Settings')
            ->add('Name','text', array('label' => "Configuration Name"))
            ->add('Language', 'choice', array(
                'label' => 'System Language',
                'choices' => array(0 => 'English', 1 => 'Swedish'),
                'preferred_choices' => array(0),
                ))
            ->add('commonmail','text', array('label' => "Common e-Mail"))
            ->add('dateformat','text', array('label' => "Date format"))
            ->add('currencyformat','text', array('label' => "Currency format"))
        ->end()
}

public function postUpdate($object) {

      // here i need to perform some validations and set flash message if there is an errror 

}

}

appreciate your help


Solution

  • You are talking about an admin class, not a controller.

    And this is not possible by default. Best way to do this is to write a custom CRUDController (extend from default one) and handle it there.