Symfony2验证 - 没有为约束配置默认选项

时间:2013-11-05 09:53:58

标签: validation symfony

我正在尝试配置约束验证。

使用Symfony \ Component \ Validator \ Constraint;

我的禁忌:

<?php
namespace Acme\MyBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class OverlapBroadcasting extends Constraint
{
    public $message = 'overlap date';

    public function validatedBy()
    {
        return "overlap_broadcasting";
    }

}

我的验证员:

<?php
namespace Acme\MyBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;


class OverlapBroadcastingValidator extends ConstraintValidator
{

    private $entityManager;

    public function __construct( $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function validate($value, Constraint $constraint)
    {
        var_dump($value);
    }

}

我的validation.yml:

Acme\MyBundle\Entity\Broadcasting:
    properties:
        start_time:
            - Acme\MyBundle\Validator\Constraints\OverlapBroadcasting:
                message: truc
            - NotBlank: ~

我的service.yml:

acme.validator.overlap_broadcasting:
    class: Acme\MyBundle\Validator\Constraints\OverlapBroadcasting
    arguments: [@doctrine.orm.entity_manager]
    tags:
        - {name: validator.constraint_validator, alias: overlap_broadcasting}

但我有这个错误:

No default option is configured for constraint Acme\MyBundle\Validator\Constraints\OverlapBroadcasting

1 个答案:

答案 0 :(得分:25)

通过将代码复制到我当前的项目中来重现错误,我相当确定问题出在services.yml中。当应该是验证器类时,该类被设置为约束类。服务定义应为:

acme.validator.overlap_broadcasting:
    class: Acme\MyBundle\Validator\Constraints\OverlapBroadcastingValidator
    arguments: [@doctrine.orm.entity_manager]
    tags:
        - {name: validator.constraint_validator, alias: overlap_broadcasting}