Symfony 2 Transfer validation to Entity, validation with related objects

I have an appeal entity; SiteTariff, Custom Objects; Tariff, sites How to transfer verification to an object. Difficulty checking: with related objects. I tried to write a callback (in Entity) => with no result.

$appeal = new Appeal(); $form = $this->createFormBuilder($appeal); if ($request->isMethod('POST')) { $form->bind($request); $appeal->setUser($user); //addRelation $appeal->appeals_count = $value; //set value to public field if ($site_tariff_id = $form->getData()->site_tariff_id) { $siteTariff = $em->find('', $site_tariff_id); $appeal->setSiteTariff($siteTariff); //addRelation } else { //addError, break } if ($appeal->getSiteTariff()->getSite()->getUser()->getId() != $user->getId()) { //addError } if ($appeal->getSiteTariff()->getTariff()->getAppealsNumber() <= $appeal->appeals_count) { //addError } if ($form->isValid()) { $em->persist($appeal); } } 

In the callback, I did not receive the $ siteTariff object after calling $this->getSiteTariff() , symfony writes a " non-object "

  public function isSiteTariffValid(ExecutionContext $context) { var_dump($this->getSiteTariff()->getId()); } 
+4
source share
1 answer

Your verification code should be inside your object, not inside your controller. Please refer to the documentation: http://symfony.com/doc/current/reference/constraints/Callback.html

0
source

Source: https://habr.com/ru/post/1440247/


All Articles