使用doctrine ORM验证约束时,Symfony表单验证将无效子表单设置为有效

时间:2016-03-11 16:27:58

标签: forms validation symfony orm doctrine

场合

我有一个包含子表单的表单类型,我在前端使用制表符进行渲染,因此一次只能显示一个子表单(如制表符)。因此,当发生不同选项卡上的错误时,我会弹出一个显示错误的对话框。通过递归循环最终请求处理的表单对象。

验证约束以表单类型编写。这会导致我可以循环的最终表单对象,并且子表单在那里发生错误时表示为 valid = false

目标

我想将所有验证约束外包到实体的ORM映射中。

问题

当我将所有验证错误外包到ORM映射中时,最终表单对象包含所有 valid = true 的子表单,即使其内部表单窗口小部件提交时也存在验证错误。只有最上面的表单级别有效= false。

这种现象发生在没有 - 以及事件订阅者的情况下构建所有子表单时。

我希望子表单在最终表单对象上也被设置为无效,当出现错误时,我该如何实现?

  • Symfony 2.8.3
  • Doctrine ORM 2.5.4

修改

使用 error_bubbling 对我不起作用。也许我没有正确使用它?

使用此输入:

form
{{ dump(form) }}

form.vars.valid
{{ dump(form.vars.valid) }}

form.vars.errors
{{ dump(form.vars.errors) }}

form.children.sections.vars.errors
{{ dump(form.children.sections.vars.errors) }}

form.children.sections.vars.valid
{{ dump(form.children.sections.vars.valid) }}

form.children.sections.children.general.vars.errors
{{ dump(form.children.sections.children.general.vars.errors) }}

form.children.sections.children.general.vars.valid
{{ dump(form.children.sections.children.general.vars.valid) }}

form.children.sections.children.general.children.lang.vars.valid
{{ dump(form.children.sections.children.general.children.lang.vars.valid) }}

form.children.sections.children.general.children.lang.vars.errors
{{ dump(form.children.sections.children.general.children.lang.vars.errors) }}

我收到了这个输出:

form
FormView {#12833 ▼
  +vars: array:28 [▶]
  +parent: null
  +children: array:3 [▶]
  -rendered: false}

form.vars.valid
false

form.vars.errors
FormErrorIterator {#12832 ▼
  -form: Form {#5652 ▶}
  -errors: array:1 [▶]
}

form.children.sections.vars.errors
FormErrorIterator {#12837 ▼
  -form: Form {#5649 ▶}
  -errors: []
}

form.children.sections.vars.valid
true

form.children.sections.children.general.vars.errors
FormErrorIterator {#12841 ▼
  -form: Form {#5498 ▶}
  -errors: []
}

form.children.sections.children.general.vars.valid
true

form.children.sections.children.general.children.lang.vars.valid
true

form.children.sections.children.general.children.lang.vars.errors
FormErrorIterator {#12851 ▼
  -form: Form {#2009 ▶}
  -errors: []
}

我在 form.children.sections.children.general.children.lang 中生成的错误。我希望从这个位置开始,递归父母有 ... valid = false

我在以下情况下尝试了 error_bubbling = true ,并使用相同的转储结果。

  • 包含 lang 小部件本身的所有表单类型
  • 没有 lang 小部件本身的所有表单类型
  • lang 小部件

我错了什么,或者我需要另一种解决方法?

编辑2

所以现在我在以下情况下尝试 error_bubbling = false 仍然相同的转储结果。

  • 包含 lang 小部件本身的所有表单类型
  • 没有 lang 小部件本身的所有表单类型
  • lang 小部件

这里有一些片段

实体

/**
 * @ORM\Entity(repositoryClass="Myvendor\CoreBundle\Entity\MyItemRepository")
 * @ORM\Table(name="myitem")
 */
class MyItem
{
   /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /**
     * @var \Myvendor\CoreBundle\Entity\Language
     *
     * @Assert\Type("Myvendor\CoreBundle\Entity\Languag")
     * @ORM\ManyToOne(targetEntity="Myvendor\CoreBundle\Entity\Language")
     * @ORM\JoinColumn(name="language_id", nullable=true, referencedColumnName="langid")
     */
    private $lang;

/**
 * Set lang
 *
 * @param \Myvendor\CoreBundle\Entity\Language $lang
 *
 * @return MyItem
 */
public function setLang(\Myvendor\CoreBundle\Entity\Language $lang = null)
{
    $this->lang = $lang;

    return $this;
}

/**
 * Get lang
 *
 * @return \Myvendor\CoreBundle\Entity\Language
 */
public function getLang()
{
    return $this->lang;
}
/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}



}

初始表单类型

class MyItemType extends AbstractMyvendorType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
         $sections = $builder->create('sections', 'form', array(
            'mapped' => false,
            'label' => false,
            'attr' => array(
                'data-sections' => '1'
            ),
            'error_bubbling' => false
          ));
        $builder->add($sections);
        $builder->addEventSubscriber(new DynamicSubforms());
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);
        $resolver->setDefaults(array(
            'error_bubbling' => false
        ));
    }
}

活动订阅者

class DynamicSubforms implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return array(
            FormEvents::PRE_SET_DATA => 'onPreSetData'
        );
    }

    /**
     * Adding subtypes, depending on the myitem type.
     *
     * @author FPI
     * @param FormEvent $event            
     */
    public function onPreSetData(FormEvent $event)
    {
        /**
         *
         * @var $data \Myvendor\CoreBundle\Entity\MyItem
         * @var $form \Symfony\Component\Form\Form
         */
        $data = $event->getData();
        $steptype = $data->getType();
        $form = $event->getForm();
        $options = $form->getConfig()->getOptions();
        $sections = $form->get('sections');

        $default_section_options = array(
            'mapped' => false,
            'data' => $data
        );
        // Every steptype has section "general"
        $sections->add('general', GeneralType::class, array_merge($default_section_options, array(
            'label' => 'general',
            'error_bubbling' => false
        )));

    }
}

子表单类型

class GeneralType extends AbstractType
{

    protected $em;

    protected $choiceOption;

    public function __construct(EntityManager $em, ChoiceOption $choice_option)
    {
        $this->em = $em;
        $this->choiceOption = $choice_option;
    }

    /*
     * Here are set the static options for each widget.
     * The dynamic options on specific widgets are specified in the event subscriber.
     * {@inheritdoc}
     *
     * @author FPI
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        /* Language */
        $builder->add('lang', 'entity', array(
            'label' => 'language',
            'class' => 'MyvendorCoreBundle:Language',
               'error_bubbling' => false
        ));

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'error_bubbling' => false
        ));
    }
}

1 个答案:

答案 0 :(得分:0)

如果您不希望将错误传递给父级表单,则应将error_bubbling设置为false,因为默认情况下error_bubbling为false ,除非表单为复合形式error_bubbling)。