0

Trying to follow the custom validator as as service example from the official docs . I have defined my validator as a service in my "services.yml":

cf.validator.unique.in.system:
            class: 'CF\AppBundle\Validator\Constraints\UniqueInSystemValidator'
            arguments: ['@doctrine']
            tags:
                - { name: 'validator.constraint_validator', alias: 'cf.validator.unique.in.system' }

I have created the neccesary constraint and validator classes:

constraint

namespace CF\AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class UniqueInSystem extends Constraint
{
    public $message = 'This value is already used.';

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function validatedBy()
    {
        return 'cf.validator.unique.in.system';
    }
}

validator

namespace CF\AppBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;

/**
 * @Annotation
 */
class UniqueInSystemValidator extends ConstraintValidator
{
    protected  $em;

    public  function  __construct(Doctrine $doctrine)
    {
        $this->em = $doctrine->getManager();
    }
    public function validate($subsystem, Constraint $constraint)
    {

        1==1; // here is a breakpoint by now
//        $this->context->addViolationAt('[expiresAt].expiresAt', $constraint->message, ['%string%' => $value->format('Y-m-d')] );
    }

What I do not understand, is the error message that I get:FatalErrorException: Error: Class '\Symfony\Component\Validator\Constraints\CF\AppBundle\Validators\Constraints\UniqueInSystem' not found in /var/www/escritorio.dev/application/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php line 89

Especially this part: '\Symfony\Component\Validator\Constraints\CF\AppBundle\Validators\Constraints\UniqueInSystem'. Why symfony tries to search in this weird namespace? How to avoid this issue?

UPD

The validator itself is applied via /src/CF/AppBundle/Resources/config/validation.yml:

CF\AppBundle\Entity\SubSystem:
  constraints:
      - CF\AppBundle\Validators\Constraints\UniqueInSystem: ~
  properties:
    name:
      - NotBlank: ~
      - Length:
          max: 100
    owner:
      - NotBlank: ~
    leader:
      - NotBlank: ~
    system:
      - NotBlank: ~
    status:
      - NotBlank : ~
      - Type:
          type: integer

The app/console container:debug output:

$ php app/console container:debug  | grep CF
auc_helper                                         container CF\AppBundle\Services\AucHelper
cf.validator.unique.in.system                      container CF\AppBundle\Validator\Constraints\UniqueInSystemValidator
7
  • Could you check please what will be the result of class_exists('CF\AppBundle\Validators\Constraints\UniqueInSystem') (somewhere in controller) Commented Aug 11, 2015 at 13:00
  • Are you sure cf.validator.unique.in.system is properly injected to the container? If symfony cannot find validator by alias it will try to include class with the provided validator name. Commented Aug 11, 2015 at 13:00
  • The problem is in the way you are using the constraint. I'm guessing you need a use statement. Update your question with an example of how you are trying to apply the constraint. Commented Aug 11, 2015 at 13:07
  • I checked the service container within the app/console container:debug and I see it loaded Commented Aug 11, 2015 at 13:07
  • Added the usage example, and output from container:debug Commented Aug 11, 2015 at 13:14

1 Answer 1

8
// This
CF\AppBundle\Entity\SubSystem:
  constraints:
    - CF\AppBundle\Validators\Constraints\UniqueInSystem: ~

// Should be
CF\AppBundle\Entity\SubSystem:
  constraints:
    - CF\AppBundle\Validator\Constraints\UniqueInSystem: ~

It's always been a bit of a mystery as to why the Constraints directory is not called Constraint. But Validator has always been called Validator.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.