/** * @param Form $form * @param Field $field * @param string $message * * @throws ValidationError */ public function __invoke(Form $form, Field $field, $message = "") { if ($field->data !== null) { $length = strlen($field->data); } else { $length = 0; } if ($length < $this->min || $this->max != -1 && $length > $this->max) { $message = $this->message; if ($message == "") { if ($this->max == -1) { if ($this->min > 1) { $message = "Field must be at least {$this->min} characters long."; } else { $message = "Field must be at least {$this->min} character long."; } } elseif ($this->min == -1) { if ($this->max > 1) { $message = "Field cannot be longer than {$this->max} characters long."; } else { $message = "Field cannot be longer than {$this->max} character long."; } } else { $message = "Field must be between {$this->min} and {$this->max} characters long."; } } else { $message = vsprintf_named($message, ["min" => $this->min, "max" => $this->max]); } throw new ValidationError($message); } }
/** * @param Form $form * @param Field $field * @param string $message * * @throws ValidationError */ public function __invoke(Form $form, Field $field, $message = "") { if ($form->{$this->fieldname} !== null) { /** @var Field $other */ $other = $form->{$this->fieldname}; } else { throw new ValidationError("Invalid field name '{$this->fieldname}'"); } if ($field->data != $other->data) { $message = $this->message; $d = ["other_name" => $this->fieldname, "other_label" => property_exists($other, 'label') && property_exists($other->label, 'text') ? $other->label->text : $this->fieldname]; if ($message == "") { $message = "Field must be equal to %(other_name)s"; } throw new ValidationError(vsprintf_named($message, $d)); } }
/** * @param Form $form * @param Field $field * @param string $message * * @throws ValidationError */ public function __invoke(Form $form, Field $field, $message = "") { $data = $field->data; if (is_null($data) || !is_null($this->min) && $data < $this->min || !is_null($this->max) && $data > $this->max) { $message = $this->message; if ($message == "") { if (is_null($this->max)) { $message = "Number must be at least {$this->min}."; } elseif (is_null($this->min)) { $message = "Number must be at most {$this->max}."; } else { $message = "Number must be between {$this->min} and {$this->max}."; } } else { $message = vsprintf_named($message, ["min" => $this->min, "max" => $this->max]); } throw new ValidationError($message); } }
function htmlLoopNamed($arr, $start, $format, $end) { $html = "\n"; $html .= $start; foreach ($arr as $items) { $html .= vsprintf_named($format, $items); } return $html . $end . "\n"; }