The constraint cannot be applied to properties or getters

I am trying to create a custom constraint in symfony 3

I have a mistake

Restriction App \ Bundle \ MyBundle \ Validator \ Constraints \ Code cannot be used for properties or getters

<?php // vendor/app/mybundle/Validator/Constraints/Code.php namespace App\Bundle\MyBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; class Code extends Constraint { public $message = 'Error validate message!!!'; public function validatedBy() { return 'alias_name'; } public function getTargets() { //return self::CLASS_CONSTRAINT; // error "The constraint cannot be put on properties or getters" return array(self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT); // working, but $value is a STRING!!! } } 

My provider / application / mybundle / Validator / Constraints / CodeValidator.php

 <?php // vendor/app/mybundle/Validator/Constraints/CodeValidator.php namespace App\Bundle\MyBundle\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; class CodeValidator extends ConstraintValidator { public function validate($value, Constraint $constraint) { $value->getId(); // this code not working, because $value is STRING!! $this->context->buildViolation($constraint->message) ->atPath('code') ->addViolation(); } 

}

Where did I make a mistake?

+5
source share
2 answers

Try adding this to your restriction:

 /** * @Annotation * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) */ 

EDIT

This morning I took the time to try to implement a new installation of symfony 3.

Also, if you added @AcmeAssert\Code to a property similar to the following:

 use AppBundle\Validator\Constraints as AcmeAssert; // ... /** * @var string * @AcmeAssert\Code * @ORM\Column(name="code", type="string") */ private $code; 

Only the field will be checked, so $value represents the value of the property field.
For example, if you assign @AcmeAssert\Code in the $code property in your entity when the form is submitted, the $value your validator will represent the value of the field of your $code property).

If you add @AcmeAssert\Code on top of your entity, as shown below:

 use AppBundle\Validator\Constraints as AcmeAssert; /** * Foo * * @ORM\Table(name="foo") * @AcmeAssert\Code * @ORM\Entity(repositoryClass="AppBundle\Repository\FooRepository") */ class Foo 

Then the $ value will represent your complete object, and you can do all the necessary checks using getters.

I added my test project to the repository, you can use it to see how to use two alternatives: sf3-constraint-test

In my example, the AppBundle::Bar object has a restriction on the property, and AppBundle::Foo has a restriction on the whole entity.

Hope you need it!

EDIT2

If you are in a Yamal mapping, use the following to apply a constraint to the entire entity and access the full object in $value in your constraint:

 AppBundle\Entity\AcmeEntity: constraints: - App\Bundle\MyBundle\Validator\Constraints\Code: ~ 

At the top of your display.

And if you just want to apply the restriction to one property and access the field value using $value , stay as you do and execute your logic on $value , which is a string (the value of the field corresponding to the property of the object limited by the display) this is something like:

 AppBundle\Entity\AcmeEntity: properties: # ... code: - App\Bundle\MyBundle\Validator\Constraints\Code: ~ 
+6
source

You have created a constraint that should only apply to the whole object, for example:

 /** * @AppAssert\SectionCode */ class MyEntity { // ... } 

If you want your restriction to apply to class members, completely remove your getTargets() function.

+4
source

Source: https://habr.com/ru/post/1242132/


All Articles