【问题标题】:Symfony2 validation doesn't work when Entity Relationships/Associations当实体关系/关联时 Symfony2 验证不起作用
【发布时间】:2014-08-26 04:42:41
【问题描述】:

控制器

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

My\SampleBundle\Entity\First:
    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

My\SampleBundle\Entity\Second:
    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' => 'My\SampleBundle\Entity\First',
        );
    }

    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

My\SampleBundle\Entity\First:
    properties:
        title:
            - NotBlank: ~
            - MinLength: 2

My\SampleBundle\Entity\Second:
    properties:
        review:
            - NotBlank: ~
            - MinLength: 14

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

如果单独执行,验证将正常进行。

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

但是,如果它处于 Entity Relationships/Associations 状态,第一次验证将不起作用。第一个字符的 title 属性将被注册。

我怎样才能做到这一点?

【问题讨论】:

  • 环境是 Symfony 2.1.2。
  • 如果你不介意的话,只标记到 symfony2.1

标签: php validation associations entity symfony-2.1


【解决方案1】:

Symfony 2.1+ 不会自动验证所有嵌入的对象。您需要将Valid constraint 放在first 字段上以使其也生效:

My\SampleBundle\Entity\Second:
    properties:
        first:
            - Valid: ~

【讨论】:

  • 哦,我明白了!它完全有效。感谢您给我的所有建议和帮助。
  • +1 好答案@elnur。很高兴在集合验证部分的文档中看到这一点。也许我错过了。
  • +1,这确实可以在本书的验证章节中得到更好的记录
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 2015-04-18
相关资源
最近更新 更多