0

有任何方法可以知道父表单是否有效。这是代码。它总是抛出:“您不能在未绑定的表单上调用 isValid()。”。谢谢!!

class FoorType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('field_1') // Required
            ->add('field_2') // Required
            ->add('field_3') // Required
            ->add('bar', 'my_custom_form_type') // Not required
    }
} 

class MyCustomFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::POST_BIND, function(DataEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();

            if ($form->getParent()->isValid()) {
                // Do smething...
            }
        });
    }

    public function getParent()
    {
        return 'text';
    }

    public function getName()
    {
        return 'my_custom_form_type';
    }
}
4

1 回答 1

0

嗨找到了解决方案!

在 addEventListener 中使用 getParent():

$builder->getParent()->addEventListener(FormEvents::POST_BIND, function(DataEvent $event)   
{
     // Code
});
于 2012-12-14T17:28:06.083 回答