Sonata Admin: How to change the form redirect from inside the admin class

1.3k Views Asked by At

I am using sonata admin bundle. I'd like to know how to change the form redirect on a certain condition through the overridden create function in my Admin Class:

    namespace PersonnelBundle\Admin\PersonnelAffairs;

    use Sonata\AdminBundle\Admin\Admin;
    use PersonnelBundle\Entity\Employee\EmployeeVacationEntry;
    //use Symfony\Component\HttpFoundation\RedirectResponse;

        class VacationEntryAdmin extends Admin
        {
         //
        //+
        //+
        //+
        ///
            public function create($c)
                {
                    $ret = $this->prePersist($c);

                    if (false === $ret) {
                        $this->getRequest()->getSession()->getFlashBag()->add("danger", 'no ');
        // I'd like to place my custom redirect here:
        // return new RedirectResponse($this->getRequest()->headers->get('referer'));
     //But that doesn't work as we are not inside the controller

                    } else {
                        foreach ($this->extensions as $extension) {
                            $extension->prePersist($this, $c);
                        }

                        $this->getModelManager()->create($c);

                        $this->postPersist($c);
                        foreach ($this->extensions as $extension) {
                            $extension->postPersist($this, $c);
                        }
                    }
                    return $c;
                }
        //
        //+
        //+
        //+
        ///
    public function prePersist($cv)
    {
//
//+
//+
//+
///
$validator = $this->getValidator();
            $errors = $validator->validate($employeeVacationEntry);
            if (count($errors) > 0) {
                foreach ($errors as $error) {
                    $errorsString = $error->getMessage();
                    $employeeName = $error->getRoot()->getEmployee()->getName();
                    $this->getRequest()->getSession()->getFlashBag()->add("danger", $employeeName . ': ' . $errorsString);
                }
                return FALSE;
            } else {
                $em->persist($employeeVacationEntry);
                return TRUE;
            }
//
//+
//+
//+
///
        }

Any Idea as how to do this ?? I also want to know the difference between the function create() in the Admin Class and the createAction() located in the CRUDController

0

There are 0 best solutions below