10

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

使用 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);
    }

}

我的验证.yml:

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

我的服务.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
4

1 回答 1

27

通过将代码复制到我当前的项目中来重现错误后,我相当确定问题出在 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}
于 2013-11-05T12:24:49.947 回答