Пример #1
0
 public function validate(ModelInterface $model, $data, $exclusions = array())
 {
     $entityName = $model->getEntityName();
     $fields = $this->entityConfig[$entityName]['validation'];
     foreach ($fields as $field => $constraints) {
         $constraints = explode('|', $constraints);
         if (!array_key_exists($field, $data)) {
             throw new AttributMissingException("The field '{$field}' is missing.");
         }
         foreach ($constraints as $constraint) {
             if (!in_array($constraint, $exclusions)) {
                 $class = 'Moulino\\Framework\\Validation\\Constraint\\' . ucfirst($constraint) . 'Validator';
                 if (class_exists($class)) {
                     $constraintValidator = new $class($this->translator);
                     $violation = $constraintValidator->validate($field, $data[$field], $model);
                     if ($violation instanceof ConstraintViolationInterface) {
                         $this->violationList->add($violation);
                     }
                 } else {
                     throw new ConstraintUndefinedException("The constraint '{$constraint}' does not exist.");
                 }
             }
         }
     }
     return $this->violationList;
 }
Пример #2
0
 public function validate($field, $value, ModelInterface $model)
 {
     if (empty($value)) {
         return;
     }
     $count = $model->count(array($field => $value));
     if ($count > 0) {
         return new ConstraintViolation($field, $this->translator->tr("This value already exists"));
     }
 }