当实体关系/关联时 Symfony2 验证不起作用 [英] Symfony2 validation doesn't work when Entity Relationships/Associations

查看:23
本文介绍了当实体关系/关联时 Symfony2 验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器

public function indexAction(Request $request)
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    $owner = $user->getId();

    $first = new First();
    $first->setOwner($owner);

    $second = new Second();
    $second->setOwner($owner);
    $second->setFirst($first);

    $form = $this->createForm(new SecondType(), $second);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->get('doctrine')->getEntityManager();
            $em->persist($first);
            $em->persist($second);
            $em->flush();
        }
    }

    return $this->render('MySampleBundle:Home:index.html.twig', array(
        'form' => $form->createView(),
    ));

}

ORM Yaml

MySampleBundleEntityFirst:
    type: entity
    table: first
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        title:
            type: string
        date_created:
            type: datetime
        date_edited:
            type: datetime
        owner:
            type: integer
    lifecycleCallbacks:
        prePersist: [ prePersist ]
        preUpdate: [ preUpdate ]
    oneToMany:
        reviews:
            targetEntity: Second
            mappedBy: review

MySampleBundleEntitySecond:
    type: entity
    table: second
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        review:
            type: string
        date_created:
            type: datetime
        date_edited:
            type: datetime
        owner:
            type: integer
    lifecycleCallbacks:
        prePersist: [ prePersist ]
        preUpdate: [ preUpdate ]
    manyToOne:
        first:
            targetEntity: First
            inversedBy: reviews
            joinColumn:
                name: first_id
                referencedColumnName: id

形式/类型

class FirstType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', 'text');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'MySampleBundleEntityFirst',
        );
    }

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

class SecondType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('first', new FirstType());
        $builder->add('review', 'textarea');
    }

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

验证.yml

MySampleBundleEntityFirst:
    properties:
        title:
            - NotBlank: ~
            - MinLength: 2

MySampleBundleEntitySecond:
    properties:
        review:
            - NotBlank: ~
            - MinLength: 14

创建的表单正常工作.但是只有验证不正常.

The created form works normally. However, only the validation does not work normally.

如果单独执行,验证将正常工作.

If it performs individually, validation will work normally.

$form = $this->createForm(new FirstType(), $first);

但是,如果它处于实体关系/关联状态,则第一个验证将不起作用.第一个字符中的标题属性将被注册.

However, if it is in a Entity Relationships/Associations state, the first validation will not work.The First's title property in one character will be registered.

我怎样才能做到这一点?

How can I manage to achieve that?

推荐答案

Symfony 2.1+ 不会自动验证所有嵌入的对象.您需要将 Valid 约束放在 first 字段使其也验证:

Symfony 2.1+ doesn't automatically validate all the embedded objects. You need to put the Valid constraint on the first field to make it validate as well:

MySampleBundleEntitySecond:
    properties:
        first:
            - Valid: ~

这篇关于当实体关系/关联时 Symfony2 验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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