简单的Symfony2 / Doctrine验证不起作用

时间:2013-07-07 02:38:38

标签: symfony doctrine-orm doctrine

这没有意义。我似乎无法通过简单的Symfony2验证工作。

$insert = new MyEntity();
$insert->setTest1( 'test' );
$validator = $this->get('validator');
$errors = $validator->validate($insert);

...但是$ errors总是一个带有空约束数组的对象。它永远不会失败验证。

我的配置(Yaml):

MyBundle\Entity\MyEntity:
    properties:
        test1:
            - MinLength: 10
            - Email
    type: entity
    table: null
    fields:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
        test1:
            type: string
            length: 255
            column: test_1
        test2:
            type: integer
            column: test_2
    lifecycleCallbacks: {  }

1 个答案:

答案 0 :(得分:4)

您在单个yml文件中混合了doctrine的映射和symfony的验证

yml中的validation configuration是从文件中加载的:

Acme/YourBundle/Resources/config/validation.yml  // YAML

Acme/YourBundle/Resources/config/validation.xml  // XML

mapping information应放在以下其中一个中:

Acme/YourBundle/Resources/config/doctrine/MyEntity.orm.yml  // YAML

Acme/YourBundle/Resources/config/doctrine/MyEntity.orm.xml  // XML

Acme/YourBundle/Resources/config/doctrine/orm/MyEntity.orm.yml // YAML

Acme/YourBundle/Resources/config/doctrine/orm/MyEntity.orm.xml // XML