我正在使用sonata admin bundle。 我想知道如何通过管理类中重写的create函数更改某个条件下的表单重定向:
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;
}
//
//+
//+
//+
///
}
任何想法如何做到这一点? 我还想知道Admin Class中的create()函数和CRUDController中的createAction()之间的区别