2

I am using Symfony2 and I am trying to build a custom validator for my class.

I followed the guide found Here

I am receiving a weird error:

FatalErrorException: Compile Error: Cannot redeclare class Acme\MyProject\Validator\Constraints\isUniqueNameInPlaces in /home/myfolder/src/Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php line 123

However, file isUniqueNameInPlaces.php is only 14 lines long. I am sure I did something stupid, but I can not find out what. Can someone give me a hand?

Here is my isUniqueNameInPlaces

  <?php

  // src/Acme/MyProject/Validator/Constraints/UniqueNameInPlaces.php
  namespace Acme\MyProject\Validator\Constraints;

  use Symfony\Component\Validator\Constraint;

  /**
  * @Annotation
  */
  class isUniqueNameInPlaces extends Constraint
  {
      public $message = 'Name is not unique';
  }

And here is its validator

  <?php

  // src/Acme/MyProject/Validator/Constraints/isUniqueNameInPlacesValidator.php
  namespace Acme\MyProject\Validator\Constraints;

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

  class isUniqueNameInPlacesValidator extends ConstraintValidator
  {

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

      public function validate($protocol, Constraint $constraint)
      {
  //      my logic
      }
  }
1
  • are you sure the name of the 2 files reflects the class names? Commented Jan 13, 2015 at 12:24

1 Answer 1

1

You already have a isUniqueNameInPlaces class elsewhere ? Thus the error Cannot redeclare class.
Give your isUniqueNameInPlaces class some other name and see if it's ok then.

Edit :

Also Shouldn't your file be
Acme/MyProject/Validator/Constraints/isUniqueNameInPlaces.php
rather than
Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php ?

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

5 Comments

comment your class, empty your cache and try again. What happens ?
If its not better then blast you cache the hard way : rm -rf app/cache/* and try again...
Commented constraint class and I get: RuntimeException: The autoloader expected class "Acme\MyProjectBundle\Validator\Constraints\isUniqueNameInPlaces" to be defined in file "/home/myfolder/src/Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
Shouldn't your file be Acme/MyProject/Validator/Constraints/isUniqueNameInPlaces.php rather than Acme/MyProjectBundle/Validator/Constraints/isUniqueNameInPlaces.php ? See MyProject instead of MyProjectBundle ?
Yes... you are right! I knew it was a stupid mistake... Thank you very much!

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.