Sonata Admin: Prevent a persist for a specific condition

560 views Asked by At

I am using sonata Admin. I have Two Entities: Vacation Entry + Employee Vacation Entry (1:m)

In my vacation entry admin class:

  • The user selects employees (from the many to many relationship) to which I'd like to give the vacation.
  • A validation is done over each employee through the Employee Vacation Entry entity.

    class VacationEntryAdmin extends Admin {
    
        // some 
        // content
        public function prePersist($cv)
        {
            // some 
            // content
            $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);
                }    //I'd like to stop the persistence of all the Vacation Entry here.
            }
            $em->persist($employeeVacationEntry);
        }
    
    }
    
0

There are 0 answers