Validator perso, passer un paramètre
Bonjour,
Dans mon développement je crées mes propre validateurs, jusqu'a présent tout fonctionne.
Ici je dois passer un paramètre de mon entity et dans ce contexte je ne peut passer d'objet !
Voici mes classe
BeforeDate
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
<?php
namespace MaitrePylos\Validator\Constraints ;
use Symfony\Component\Validator\Constraint;
class BeforeDate extends Constraint {
public $message = 'La date de fin ne peut-être inférieur à la date de début ! ' ;
public $date ;
public function __construct($date){
$this->date = $date;
}
}
?> |
BeforeDateValidator
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
<?php
namespace MaitrePylos\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class BeforeDateValidator extends ConstraintValidator {
public function isValid($value, Constraint $constraint) {
if($value->format('Ymd') < $constraint->date->format('Ymd')){
$this->setMessage($constraint->message, array('{{ value }}' => $value));
return false;
}
return true;
}
}
?> |
et dans mon entity
Code:
1 2 3 4 5
|
public static function loadValidatorMetadata(ClassMetadata $metadata) {
$metadata->addPropertyConstraint('dDateFinContrat', new MP\BeforeDate(array('date'=>$this->dDateDebutContrat)));
} |
ici $this->dDateDebutContrat ne peut être entré dans ce contexte.
Une idée de comment passer ce paramètre ?
merci