Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 validation - No default option is configured for constraint

I'm trying to configure a constraint validation.

use Symfony\Component\Validator\Constraint;

My contraint:

<?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";
    }

}

My validator:

<?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);
    }

}

my validation.yml:

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

my 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}

But I have this errror :

No default option is configured for constraint Acme\MyBundle\Validator\Constraints\OverlapBroadcasting
like image 869
wonzbak Avatar asked Nov 05 '13 09:11

wonzbak


1 Answers

Having reproduced the error by copying the code into my current project I'm fairly certain the problem is in the services.yml. The class is set to the constraint class when it should be the validator class. The service definition should be:

acme.validator.overlap_broadcasting:
    class: Acme\MyBundle\Validator\Constraints\OverlapBroadcastingValidator
    arguments: [@doctrine.orm.entity_manager]
    tags:
        - {name: validator.constraint_validator, alias: overlap_broadcasting}
like image 145
redbirdo Avatar answered Nov 03 '22 01:11

redbirdo