With symfony2 Is it possible to use constraintValidator service to validate properties

constraints, custom-validators, symfony, validation

Solution

You should change the return value of `getTargets()` to `self::PROPERTY_CONSTRAINT`. Right now it is `self::CLASS_CONSTRAINT`, which means that the constraint can only be put on the class level, but not on the property level (e.g. the property `$maxTotalParticipant` in your case).

Problem

I followed the following Symfony2 cookbook tutorial , but I keep getting the error `constraint myContraintClass cannot be put on properties or getters` in validation.yml I have ``` Core\Entity\Activity: properties: maxTotalParticipant: - Application\ActivityBundle\Validator\ActivityMaxTotalParticipant: ~ ``` here is the my Constraint ``` <?php namespace Application\ActivityBundle\Validator; use Symfony\Component\Validator\Constraint; /** * @Annotation */ class ActivityMaxTotalParticipant extends Constraint { public $message = 'trigger error'; public function validatedBy() { return 'activity_max_total_participant_validator'; } public function getTargets() { return self::CLASS_CONSTRAINT; } public function getMessage() { return $this->message; } } ``` and my Constraint Validator ``` <?php namespace Application\ActivityBundle\Validator; use Symfony\Component\Validator\ExecutionContext; use Symfony\Bundle\DoctrineBundle\Registry; use myApp\Core\Entity\Activity; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Translation\Translator; use Symfony\Component\Translation; class ActivityMaxTotalParticipantValidator extends ConstraintValidator { private $doctrine; private $translator; public function __construct(Registry $doctrine, Translator $translator){ $this->doctrine = $doctrine; $this->translator = $translator; } /** * @param : $value {Activity object} * @param : $constraint {ActivityMaxTotalParticipant object} * */ public function isValid( $value, Constraint $constraint) { if($value->getId() !== null) { return true; } return false; } private function isMaxTotalParticipantGreaterThanActualParticipants(Activity $activity, ExecutionContext $context){ $activityUser = $this->doctrine->getEntityManagerForClass('myApp\Core\Entity\ActivityUser')->getRepository('myApp\Core\Entity\ActivityUser')->countNumberParticipantByActivityId($value->getId()); if($activityUser > $activity->getMaxTotalParticipant() && $activity->getMaxTotalParticipant() !== null) { $propertyPath = $this->context->getPropertyPath().'.maxTotalParticipant'; $this->context->setPropertyPath($propertyPath); $this->context->addViolation($constraint->message, array(), null); return false; } return true; } private function isMaxTotalParticipantGreaterEqualOne(Activity $activity, ExecutionContext $context) { $maxTotalParticipant = $activity->getMaxTotalParticipant(); if ($activity->getNumberOfPlacesLimited() == 1 && ($maxTotalParticipant == null || trim($maxTotalParticipant) == '' || $maxTotalParticipant < 1) ) { return false; } return true; } } ```

Original source