2016-10-09 3 views
1

내 프로젝트에 이벤트, OrganizerProfile 및 User라는 세 개의 문서가 있습니다.Symfony3 : 중첩 된 (선택 사항) 양식 유효성 확인

사용자는 여러 OrganizerProfile (Facebook의 '페이지'와 같음) 및 이벤트를 가질 수 있습니다.

사용자가 만들고 이벤트를하면 "OrganizerProfile"을 이벤트에 할당 할 수 있습니다 (사용자 Alberto가 "이벤트 X"라는 "회사 A"에 대한 이벤트를 만듭니다).

OrganizerProfileType.php

class OrganizerProfileType extends AbstractType{ 
    public function buildForm(FormBuilderInterface $builder, array $options){ 
     $builder 
      ->add('email', EmailType::class) 
      ->add('name', TextType::class) 
      ->add('description', TextType::class, ['required' => false]) 
... 

EventType.php 분야 "profile_list"사용자에

class EventType extends AbstractType { 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $profileChoices = ... //List of existing profiles 

     $builder 
      ->add('profile_list', ChoiceType::class, [ 
       'choices' => $profileChoices, 
       'required' => false, 
       'mapped' => false, 
      ]) 
      ->add('profile', OrganizerProfileType::class, [ 
       'required' => true, 
      ]) 
      ->add('title', TextType::class, ['required' => false]) 
      ->add('description', TextType::class, ['required' => false]) 
... 

:

이를 달성하려면,이 형태를 만들었습니다 기존 OrganizerProfiles를 찾을 수 있습니다. 사용자는 그 중 하나를 선택하여 이벤트에 할당 할 수 있지만 사용자가 기존 프로파일을 선택하지 않으면 "프로파일"양식에 정보를 삽입해야합니다.

"선택 사항"을 프로필 양식으로 만들고 사용자가 기존 프로필을 선택하지 않은 경우에만 필요합니다.

어떻게하면됩니까? 감사합니다

+1

[제출 된 데이터를 기반으로 검증 그룹을 선택하십시오.] (http://symfony.com/doc/current/form/data_based_validation.html) – Matteo

답변

0

Validation Groups Based on the Submitted Data으로 구현할 수 있습니다.

그래서 FormType은 다음과 같아야합니다

class EventType extends AbstractType { 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $profileChoices = [...]; 

     $builder 
      ->add('profile_list', ChoiceType::class, [ 
       'choices' => $profileChoices, 
       'required' => false, 
       'mapped' => false, 
      ]) 
      ->add('profile', OrganizerProfileType::class, [ 
       'constraints' => [ 
        new Valid() 
       ] 
      ]) 
      ->add('title', TextType::class, ['required' => false]) 
      ->add('description', TextType::class, ['required' => false]); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'validation_groups' => function (FormInterface $form) { 

       if (null === $form->get('profile_list')->getData()) { 
        return ['without_profile']; 
       } 

       return ['with_profile']; // Or simply 'Default' 
      }, 
     ]); 
    } 

} 

UPDATE : 또한

는 그런 다음 유효성 검사 그룹과 하위 폼의 유효성 검사를 처리 할 필요가 다른 FormType에서 유효성 검사 그룹을 사용 예로서 :

class OrganizerProfileType extends AbstractType{ 

    public function buildForm(FormBuilderInterface $builder, array $options){ 
     $builder 
      ->add('email', EmailType::class, [ 
        'constraints' => [ 
         new NotNull(
          [ 
           'groups' => ['without_profile', 'with_profile'] 
          ] 
         ) 
      ]]) 
      ->add('name', TextType::class, [ 
        'constraints' => [ 
         new NotNull(
          [ 
           'groups' => ['without_profile', 'with_profile'] 
          ] 
         ) 
      ]]) 
      ->add('description', TextType::class, ['required' => false]) 

희망이 도움

+0

감사합니다. 내일 귀하의 솔루션을 사용해 보겠습니다! 만약 작동한다면 SymfonyDay에서 맥주를 ​​드릴 것입니다 (만약 당신이 거기에 있다면) –

+0

"$ form-> isValid()"메소드에 도달했을 때 "Event"문서에 유효성 검사 그룹을 넣으면 제대로 중지되지만 그는 유효성 검사 그룹을 무시하고 OrganizerProfile 양식의 유효성 검사를 건너 뜁니다. 또한 'cascade_validation'을 시도했지만 여전히 작동하지 않습니다. "수동 유효성 검사"(유효성 검사 서비스 사용 및 올바른 그룹의 OrganizerProfile 양식 유효성 검사)로만 작동합니다. 모든 양식 (및 하위 양식)을 표준 메서드 "$ form-> isValid()"로 유효성을 검사하는 방법이 있습니까? –

+0

안녕하세요 @AlbertoFecchi, 다른 FormType에도 유효성 검사 그룹을 사용해야합니다. 내 업데이트를 확인하십시오. sfday도 보길 바랍니다 :) – Matteo