dongtangu6889 2015-03-17 09:45
浏览 36
已采纳

Symfony2形成儿童和错误

I'm building a site-wide search form and I'm having trouble understanding the Symfony2 Forms structure, regarding children forms and error handling.

My code looks like this:

Form Type

class SearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('q', 'search', array(
                'label' => '',
                'attr' => array(),
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\SearchQuery',
            'csrf_protection' => false,
        ));
    }

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

Entity (referred as data_class in the Form Type above)

class SearchQuery
{
    /**
     * @Assert\NotBlank()
     * @Assert\Type(type="string", message="form.search.query.validation.type")
     * @Assert\Length(
     *      min = 3,
     *      max = 50
     * )
     */
    protected $q;

    public function __construct($q = '')
    {
        $this->setQ($q);
    }

    public function __toString() {
        return $this->getQ();
    }

    public function getQ()
    {
        return $this->q;
    }

    public function setQ($query)
    {
        $this->q = $query;
    }
}

Controller

$searchForm = $this->createForm(new SearchType());
$searchFormData = array('q' => $q);
$searchForm->submit($searchFormData);

if ($searchForm->isValid()) {
    [...]
}
else {
    // this returns an empty array regardless there are errors or not
    $errors = $searchForm->getErrors(); 

    // this returns errors when they are present
    $errorMessages = $searchForm->getErrorsAsString();
}

You might notice on the second line in the controller code, that I'm submitting an array, not a SearchQuery object. I've tried that and it doesn't come out well, although I expected this to be the right approach.

Digging in the Symfony documentation and questions here on StackOverflow helped me understand that getErrors() (see it here) never checks children forms and getErrorsAsString() (see it here) does. So I guessed that that's the reason behind the described behaviour and so it was, my form had a child form.

My question is why is there a child form present and how to get rid of it? What am I doing wrong?

Thanks!

  • 写回答

1条回答 默认 最新

  • douhe1002 2015-03-17 10:51
    关注

    Fixed by adding error_bubbling to the q field in the form type class.

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('q', 'search', array(
                'label' => '',
                'attr' => array(),
                'error_bubbling' => true,
            ))
        ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP