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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
class ValidatorAnnees extends sfValidatorSchema{
public function __construct($anneeDebut, $anneeFin, $min=1900, $max=9999, $options = array(), $messages = array()){
$this->addOption('annee_debut', $anneeDebut);
$this->addOption('annee_fin', $anneeFin);
$this->addMessage('invalid_'.$anneeDebut, 'Année de début invalide');
$this->addMessage('invalid_'.$anneeFin, 'Année de fin invalide');
$this->addOption('min', $min);
$this->addOption('max', $max);
$this->addOption('throw_global_error', false);
parent::__construct(null, $options, $messages);
}
protected function doClean($values){
if (null === $values){
$values = array();
}
if (!is_array($values)){
throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
}
$anneeDebut = isset($values[$this->getOption('annee_debut')]) ? $values[$this->getOption('annee_debut')] : null;
$anneeFin = isset($values[$this->getOption('annee_fin')]) ? $values[$this->getOption('annee_fin')] : null;
$anneeMin = $this->getOption('min');
$anneeMax = $this->getOption('max');
$error_values = array( 'annee_debut' => $anneeDebut, 'annee_fin' => $anneeFin,
'annee_min' => $anneeMin, 'annee_max' => $anneeMax );
if($anneeDebut<=$anneeMax && $anneeDebut>= $anneeMin){
if(($anneeFin===null) || ($anneeDebut<=$anneeFin && $anneeFin<=$anneeMax)){
$error_field = "";
}else{
$error_field = $this->getOption('annee_fin');
}
}else{
$error_field = $this->getOption('annee_debut');
}
if($error_field != ""){
$error = new sfValidatorError($this, 'invalid_'.$error_field, $error_values);
if ($this->getOption('throw_global_error'))
{
throw $error;
}
throw new sfValidatorErrorSchema($this, array($error_field => $error));
}
return $values;
}
} |
Partager