验证动态填充的选择字段

Dev*_*ine 6 validation symfony-forms symfony

我有两个选择领域,一个取决于另一个.

构建表单时,依赖字段有一个空选择的数组.

然后我在JavaScript中填写此字段,请求操作中的某些数据.

问题来自验证.当然它没有通过,因为单值或多值不能对空值有效.为了解决这个问题,我创建了一个PRE_BIND基本上删除的监听器,然后使用正确的值重新创建选择字段,但它仍然没有通过验证.

$form->getErrors()什么都不返回,但$form->getErrorsAsString()在我的选择字段上返回错误.

我的表格:

<?php

namespace Foo\BarBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BarFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // other fields

        // This field is filled in ajax
        $builder->add('stores', 'choice', array(
            'label' => 'form.label.stores',
            'translation_domain' => 'FooBarBundle',
            'choices' => $options['storesList'],
            'required' => false,
            'multiple' => true,
            'auto_initialize' => false,
            'attr' => array(
                'class' => 'chzn-select',
                'placeholder' => 'form.placeholder.stores'
        )));

        $func = function (FormEvent $e) use ($options) {
            $data = $e->getData();
            $form = $e->getForm();
            if ($form->has('stores')) {
                $form->remove('stores');
            }

            $brand = isset($data['brand']) ? $data['brand'] : null;

            if ($brand !== null) {
                $choices = $options['miscRepo']->getStoresNameIndexedById($brand);
                $choices = array_keys($choices);
                $choices = array_map('strval', $choices);
            } else {
                $choices = array();
            }

            $form->add('stores', 'choice', array('choices' => $choices, 'multiple' => true, 'attr' => array('class' => 'chzn-select')));
        };

        $builder->addEventListener(FormEvents::PRE_SUBMIT, $func);
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setRequired(array(
            'storesList',
            'miscRepo',
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

我有和你一样的问题:通过javascript更新字段,但没有通过验证。在 PRE_SUBMIT 事件中,读取您使用 javascript 添加的值,查询并获取具有该 ID 的对象,然后更新字段选择。

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
    $data = $event->getData();
    $form = $event->getForm();
    $pageId = $data['page_id'];

    $page = $this->myManager->getPage($pageId);
    $options = array($page->getId() => $page->getTitle());

    $form->add('page_id', 'choice', array(
        'label'         => 'Select page',
        'choices'       => $options,
        'required'      => true,
        'placeholder'   => 'Select page'
    ));

    $form->getData()->setPageId($pageId);
});
Run Code Online (Sandbox Code Playgroud)