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:
App\UserBundle\Entity\Profile:
constraints:
- App\UserBundle\Validator\Constraints\Profile: ~
properties:
type:
- NotBlank:
message: "default.notBlank"
name:
- NotBlank:
message: "default.notBlank"
firstname:
- NotBlank:
message: "default.notBlank"
groups: [person]
lastname:
- NotBlank:
message: "default.notBlank"
groups: [person]
W akcji create tworzę sobie formularz:
/**
* Creates person profile.
*
* @param Request $request
* @param $userId
* @return Response
*/
public function createPersonAction(Request $request, $userId)
{
$emObj = $this
->getDoctrine()
->getManager();
// gets user object
$userObj = $emObj->getRepository('AppUserBundle:User')
->find($userId);
// creates new object
$entity = new Profile();
// sets user
$entity->setUser($userObj);
// edit person form
$editFormObj = $this->createForm(new ProfilePersonType(),
$entity,
'validation_groups' => array( null,
'person'
)
))
// new profile has no address
->remove('profileAddress');
// services request
$editFormObj->handleRequest($request);
// checks if submitted and valid
if($editFormObj->isValid())
{
// saves
$this->container->get('app.user.profile.manager')
->save($entity);
// sets flash
$this
->get('session')
->getFlashBag()
->add(
'infoData',
'messageType' => 'msg_success',
'message' => 'handy.savedSuccessfully'
));
return $this->redirect($this->generateUrl('app_user_backend_profile_edit_person',
'userId' => $userObj->getId()
)));
}
return $this->render('AppUserBundle:Backend:Profile/createPerson.html.twig',
'userObj' => $userObj,
'entity' => $entity,
'editFormView' => $editFormObj->createView(),
'googleMapParamArr' => $this->container->getParameter('google_map')
));
}
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 (IMG:
style_emoticons/default/questionmark.gif) ?
Tutaj jeszcze formularz:
class ProfileType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$builder->add('type', 'choice', array( 'label' => 'profile.field.type',
'label_attr' => array('class' => 'control-label'), 'choices' => Profile::$typeArr
))
->add('name', null, array( 'label' => 'profile.field.name',
'label_attr' => array('class' => 'control-label') ))
->add('gender', 'choice', array( 'label' => 'profile.field.gender',
'label_attr' => array('class' => 'control-label'), 'choices' => Profile::$genderArr
))
->add('firstname', null, array( 'label' => 'profile.field.firstname',
'label_attr' => array('class' => 'control-label') ))
->add('lastname', null, array( 'label' => 'profile.field.lastname',
'label_attr' => array('class' => 'control-label') ))
->add('save', 'submit', array( 'label' => 'handy.save',
));
}
O co chodzi? Zaznaczam, że problem występuje tylko jeżeli w createForm przekaże 2 grupy... jak jedną to wszystko jest ok....