Exemplo n.º 1
0
 protected function validateCollection(string $expectType, $values, \sndsgd\form\Validator $validator) : bool
 {
     if (!$this->hasRule(FieldInterface::REQUIRED) && (is_null($values) || is_array($values) && empty($values))) {
         return false;
     }
     if (!is_array($values)) {
         $validator->addError($this->getNestedName($validator->getOptions()->getNameDelimiter()), $this->getUnexpectedTypeMessage($expectType, $values));
         return false;
     }
     # verify the parent field rules
     # these should never have any impact on the values, but should be
     # used to verify all given values are acceptable together
     foreach ($this->rules as $rule) {
         if (!$rule->validate($values, $validator)) {
             $validator->addError($this->getNestedName($validator->getOptions()->getNameDelimiter()), $rule->getErrorMessage());
             return false;
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function validate($values, \sndsgd\form\Validator $validator)
 {
     if (is_array($values)) {
         $len = count($values);
         if ($len > 1) {
             $validator->addError($this->getNestedName($validator->getOptions()->getNameDelimiter()), "expecting a single value; encountered {$len}");
             return null;
         }
         $values = $values[0];
     }
     if (is_null($values) && !$this->hasRule(\sndsgd\form\rule\RequiredRule::class)) {
         return $this->defaultValue;
     }
     foreach ($this->rules as $rule) {
         if (!$rule->validate($values, $validator)) {
             $validator->addError($this->getNestedName($validator->getOptions()->getNameDelimiter()), $rule->getErrorMessage());
             return null;
         }
     }
     return $values;
 }