Symfony2,验证具有相同参数和不同结果的嵌入式和非嵌入式表单吗? [英] Symfony2, validation embedded and non-embedded forms with same parameters and different results?

查看:77
本文介绍了Symfony2,验证具有相同参数和不同结果的嵌入式和非嵌入式表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表单生成器:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('task', 'text', array('label' => 'Task'))
                ->add('dueDate', 'date', array('label' => 'Date', 'format' => 'ddMMMMyyyy'))
                ->add('category', 'entity', array('required' => true, 'multiple' => true, 'class' => 'AcmeTaskBundle:Category', 'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },))
                ->add('save', 'submit', array('label' => 'Send'));
    }

,并且可以在此控制器上成功运行:

and it works successful with this controller:

if($form->isValid())
{
    $this->get('session')->getFlashBag()->add(
        'success',
        'Task successfuly added'
    );
    $em = $this->getDoctrine()->getManager();
    foreach($form->get('category')->getData() as $cat)
    {
        $task->removeCategory($cat);
        $task->addCategory($cat);
    }
    $em->persist($task);
    try {
        $em->flush();
    } catch (\PDOException $e) {
        // sth
    }
}

但是

如果我尝试将表单与字段类别一起嵌入:

if I try to embed my form with field category like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('task', 'text', array('label' => 'Task'))
            ->add('dueDate', 'date', array('label' => 'Date', 'format' => 'ddMMMMyyyy'))
            ->add('category', new CategoryType())
            ->add('save', 'submit', array('label' => 'Send'));
}

我的CategoryType如下:

where my CategoryType looks like:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        //'data_class' => 'Acme\TaskBundle\Entity\Category',
        'csrf_protection' => true,
    ));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', 'entity', array(
                  'class' => 'AcmeTaskBundle:Category',
                  'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },
                  //'property' => 'name',
                  'multiple' => true,
                  'required' => true,
                  ));
}

它返回预期的Category的Exception实例,给出ArrayCollection. 为什么,相同"表单只有在未嵌入的情况下才能起作用?

it returns an Exception instance of Category expected, ArrayCollection given. Why the "same" form is working only if it is not embedded?

推荐答案

这两种类型不相同.在第一种情况下,您使用选项'multiple' => true,这意味着表单需要收集Category实体.从您的控制器中,我看到您具有(一个|许多)一对多关系的任务类别.这里的Category是Category实体的ArrayCollection,因此您的表单正在工作.

These two types are not the same. In the first case you use option 'multiple' => true that means that form expects collection of Category entity. From your controller I see that you have (One|Many)-To-Many relation Task-Category. Category here is ArrayCollection of Category entities and therefore your form is working.

在第二种情况下,您具有->add('category', new CategoryType()),这意味着Category可以是唯一的类别,根据您的控制器和Task实体,这是不正确的.您需要在此处创建CategoryType()的集合.

In the second case you have ->add('category', new CategoryType()) that means that Category can be the only one, according to your controller and Task entity it is not true. You need to create collection of CategoryType() here.

->add('category', 'collection', array('type' => new CategoryType()))

此外,我认为您必须为该行提供更多选项以适合您的应用程序.

Also I think you must provide this line with more options to fit your application.

这篇关于Symfony2,验证具有相同参数和不同结果的嵌入式和非嵌入式表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆