11

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

Lasith
  • 565
  • 1
  • 6
  • 17

4 Answers4

18

Yes, you can set a flash message in an admin class. First, you can define a custom flash message type for the SonataCoreBundle. For example, if you want a success flash message type, add this in the app/config/config.yml file:

sonata_core:
    flashmessage:
        success:
            types:
                - { type: mytodo_success, domain: MyToDoBundle }

Then, you need to know when to set the message. For example, if you want to set the message after creating a new entity, you can do so overriding the postPersist function in your admin class, and adding the message in the Symfony flash bag:

public function postPersist($object) {
    $this->getRequest()->getSession()->getFlashBag()->add("mytodo_success", "My To-Do custom success message");
}

This way, the message will be displayed whenever you create a new entity at the admin class.

You can also use the default type success:

public function postPersist($object) {
    $this->getRequest()->getSession()->getFlashBag()->add("success", "My To-Do custom success message");
}
cezar
  • 11,616
  • 6
  • 48
  • 84
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
4

Because it is an Admin class I get the flashbag over the Session service:

protected function whereever()
{
    $this->getFlashBag()->add(
        'info',
        'Your message'
    );
}
...
protected function getFlashBag()
{
    return $this->getConfigurationPool()->getContainer()->get('session')->getFlashBag();
}

Cheers

binzram
  • 530
  • 4
  • 13
3

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.

Baba Yaga
  • 1,819
  • 15
  • 20
  • 8
    incorrect answer that shouldn't be the accepted one : as described in other answers, you can inject the session service to use the flashbag in any class where you can inject services, including Sonata admin classes. – Nicolas Janel Jul 27 '15 at 17:44
3

You need to inject the session service into your admin class.

http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services

rpg600
  • 2,800
  • 18
  • 24