/** * Adds a validationItem to the internal validation list * @param ValidationItem $validation */ public function addValidation(ValidationItem $validation) { $el = $validation->getElement(); $name = $el->getName(); if (null !== $name) { $this->validationsName[$el->getName(true, true)] = $validation; } $this->validationsInternalName[$validation->getElement()->getInternalName(true)] = $validation; }
/** * @inheritdoc */ public function validate(ValidationItem $validationItem) { $value = $validationItem->getLocalData()->getDataCopy(); if (!is_array($value) || !isset($value[$validationItem->getLocalName()]) || null === $value[$validationItem->getLocalName()]) { $message = new Validation\Message("Field is required", [], self::REQUIRED); $validationItem->appendMessage($message); return false; } return true; }
/** * @inheritdoc */ public function validate(ValidationItem $validationItem) { $value1 = $validationItem->getValue(); $value2 = $validationItem->findValue($this->sameElement); if ($value2 !== $value1) { $message = new Validation\Message("Fields %_tested-field_% and %_compare-field_% have different values", ["tested-field" => $validationItem->getElement()->getName(true, true), "compare-field" => $this->sameElement], self::DIFFERENT); $validationItem->appendMessage($message); return false; } return true; }
/** * @inheritdoc */ public function validate(ValidationItem $validationItem) { $value = $validationItem->getValue(); $length = strlen($value); if ($this->minLength > 0 && $length < $this->minLength) { $message = new Validation\Message('String too short (less than %_min-length_% character)', ["min-length" => $this->minLength, "string-length" => $length], self::TOO_SHORT); $validationItem->appendMessage($message); return false; } if ($this->maxLength > 0 && $length > $this->maxLength) { $message = new Validation\Message('String too long (more than %_max-length_% character)', ["max-length" => $this->maxLength, "string-length" => $length], self::TOO_LONG); $validationItem->appendMessage($message); return false; } return true; }
/** * Internal use - prepare the validation object * * @param DataContext $localValues * @param FormContext $formContext * @throws \UForm\Exception */ public function prepareValidation(DataContext $localValues, FormContext $formContext) { $validators = $this->getValidators(); $v = new ValidationItem($localValues, $this, $formContext); $v->addValidators($validators); $formContext->getChainedValidation()->addValidation($v); }