Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony][Symfony2][SF2] Walidacja formularzy - grupy
Forum PHP.pl > Forum > PHP > Frameworki
blackroger
Witam mam problem dot. walidacji, gdy jest zdefiniowanych w formularzu wiele grup. Mam wrażenie, że przy wcześniejszych wersjach sf 2 to działało bez zarzutu (aktualnie 2.5.2), teraz coś się popsuło.
Mianowicie mam takie kody:
validation.yml:
  1. App\UserBundle\Entity\Profile:
  2. constraints:
  3. - App\UserBundle\Validator\Constraints\Profile: ~
  4. properties:
  5. type:
  6. - NotBlank:
  7. message: "default.notBlank"
  8. name:
  9. - NotBlank:
  10. message: "default.notBlank"
  11. firstname:
  12. - NotBlank:
  13. message: "default.notBlank"
  14. groups: [person]
  15. lastname:
  16. - NotBlank:
  17. message: "default.notBlank"
  18. groups: [person]



W akcji create tworzę sobie formularz:
  1. /**
  2.   * Creates person profile.
  3.   *
  4.   * @param Request $request
  5.   * @param $userId
  6.   * @return Response
  7.   */
  8. public function createPersonAction(Request $request, $userId)
  9. {
  10. $emObj = $this
  11. ->getDoctrine()
  12. ->getManager();
  13.  
  14. // gets user object
  15. $userObj = $emObj->getRepository('AppUserBundle:User')
  16. ->find($userId);
  17.  
  18. // creates new object
  19. $entity = new Profile();
  20.  
  21. // sets user
  22. $entity->setUser($userObj);
  23.  
  24. // edit person form
  25. $editFormObj = $this->createForm(new ProfilePersonType(),
  26. $entity,
  27. 'validation_groups' => array(
  28. null,
  29. 'person'
  30. )
  31. ))
  32. // new profile has no address
  33. ->remove('profileAddress');
  34.  
  35. // services request
  36. $editFormObj->handleRequest($request);
  37.  
  38. // checks if submitted and valid
  39. if($editFormObj->isValid())
  40. {
  41. // saves
  42. $this->container->get('app.user.profile.manager')
  43. ->save($entity);
  44.  
  45. // sets flash
  46. $this
  47. ->get('session')
  48. ->getFlashBag()
  49. ->add(
  50. 'infoData',
  51. 'messageType' => 'msg_success',
  52. 'message' => 'handy.savedSuccessfully'
  53. ));
  54.  
  55. return $this->redirect($this->generateUrl('app_user_backend_profile_edit_person',
  56. 'userId' => $userObj->getId()
  57. )));
  58. }
  59.  
  60. return $this->render('AppUserBundle:Backend:Profile/createPerson.html.twig',
  61. 'userObj' => $userObj,
  62. 'entity' => $entity,
  63. 'editFormView' => $editFormObj->createView(),
  64. 'googleMapParamArr' => $this->container->getParameter('google_map')
  65. ));
  66. }


Przy pustych polach formularza dostaje 3 błędy (prawidłowo), ale jak zerknę w debug toolbara to widzę:
dla name:
Constraint Violation
Object(Symfony\Component\Form\Form).data.name = null

a dla gender 2 błędy:
Constraint Violation
Object(Symfony\Component\Form\Form).data.gender.data.firstname = null
Constraint Violation
Object(Symfony\Component\Form\Form).data.gender.data.lastname = null


I tu zadaje pytanie:
Dlaczego jest takie mapowanie: .data.gender.data.lastname, przecież powinno być .data.lastname i .data.firstname questionmark.gif?

Tutaj jeszcze formularz:
  1. class ProfileType extends AbstractType
  2. {
  3. /**
  4.   * @param FormBuilderInterface $builder
  5.   * @param array $options
  6.   */
  7. public function buildForm(FormBuilderInterface $builder, array $options)
  8. {
  9. $builder->add('type', 'choice', array(
  10. 'label' => 'profile.field.type',
  11. 'label_attr' => array('class' => 'control-label'),
  12. 'choices' => Profile::$typeArr
  13. ))
  14. ->add('name', null, array(
  15. 'label' => 'profile.field.name',
  16. 'label_attr' => array('class' => 'control-label')
  17. ))
  18. ->add('gender', 'choice', array(
  19. 'label' => 'profile.field.gender',
  20. 'label_attr' => array('class' => 'control-label'),
  21. 'choices' => Profile::$genderArr
  22. ))
  23. ->add('firstname', null, array(
  24. 'label' => 'profile.field.firstname',
  25. 'label_attr' => array('class' => 'control-label')
  26. ))
  27. ->add('lastname', null, array(
  28. 'label' => 'profile.field.lastname',
  29. 'label_attr' => array('class' => 'control-label')
  30. ))
  31. ->add('save', 'submit', array(
  32. 'label' => 'handy.save',
  33. ));
  34. }


O co chodzi? Zaznaczam, że problem występuje tylko jeżeli w createForm przekaże 2 grupy... jak jedną to wszystko jest ok....
destroyerr
Link.
Rozwiązanie problemu: w pliku config.yml należy ustawić:
Kod
validation:
    enable_annotations: true
    api: 2.4
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.