2017-10-13 112 views
1

我有一個項目實體,它有許多圖像,每個圖像都有標題,圖像文件屬性爲Symfony 3中的每個項目指定不同的驗證組?

我希望用戶能夠添加,更新和刪除項目表單中的圖像。

問題是projectImage實體驗證組在新的時候應該與編輯時不同。

,這是代碼

項目類型

class ProjectType extends AbstractType 
{ 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->addEventSubscriber(new \ControlPanelBundle\Form\EventListener\CityFieldSubscriber()) 
      ->addEventSubscriber(new \ControlPanelBundle\Form\EventListener\CountryFieldSubscriber()); 

     $builder 
      ->add('title') 
      ->add('description') 
      ->add('area') 
      ->add('startDate') 
      ->add('deliveryDate') 
      ->add('phases') 
      ->add('partners', EntityType::class, [ 
       'class'   => 'AppBundle:Partner', 
       'multiple'  => true, 
       'query_builder' => function (EntityRepository $er) { 
        return $er->createQueryBuilder('p') 
         ->orderBy('p.title', 'ASC'); 
       } 
      ]) 
      ->add('projectImages', CollectionType::class, [ 
       'entry_type'  => ProjectImageType::class, 
       'allow_add'  => true, 
       'allow_delete' => true, 
       'delete_empty' => true, 
       'by_reference' => false, 
       'error_bubbling' => false 
     ]); 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Project' 
     )); 
    } 

    /** 
    * @return string 
    */ 
    public function getName() 
    { 
     return 'project'; 
    } 
} 

ProjectImageType

class ProjectImageType extends AbstractType 
{ 

    /** 
    * @param FormBuilderInterface $builder 
    * @param array $options 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('id', HiddenType::class) 
      ->add('title', TextType::class) 
      ->add('type', ChoiceType::class, ['choices' => array_flip(\AppBundle\Entity\ProjectImage::getTypeChoices())]) 
      ->add('imageFile', FileType::class); 
    } 

    /** 
    * @param OptionsResolver $resolver 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'data_class'  => 'AppBundle\Entity\ProjectImage', 
      'validation_groups' => function(\Symfony\Component\Form\FormInterface $form) { 
       $validationGroups = ['Default']; 

       if ($form->getData() === null || null === $form->getData()->getId()) { 
        $validationGroups[] = "newImage"; 
       } 

       return $validationGroups; 
      } 
     ]); 
    } 

    public function getName() 
    { 
     return 'project_image'; 
    } 
} 

,你可以在ProjectImageType看到我添加驗證組 「newImage」如果圖像是新的或者如果用戶想要添加新圖像,在這種情況下只有im年齡文件應該是必需的,並且如果用戶只想更新圖像的標題就不需要。

問題是我在ProjectImageType中添加的驗證組「newImage」總是被忽略。

我想在這裏Specify different validation groups for each item of a collection in Symfony 2? 提供的解決方案,但問題是,從symfony的刪除cascade_validation選項3

這裏的問題是如何能夠從彼此也不同驗證對不同的驗證組每個集合類型實體從父窗體類型驗證組?

回答

3

我解決了這個問題,而無需使用驗證組由項目單位增加一個回調確認如下:

ProjectImage實體

/** 
* assert that image not blank only if new object 
* 
* @Assert\Callback 
*/ 
public function validateImage(ExecutionContextInterface $context) { 
    if (null === $this->id && null === $this->getImageFile()) { 
     $context->buildViolation('Image is required') 
       ->atPath('imageFile') 
       ->addViolation(); 
    } 
} 
相關問題