randomphp/validation

该包已被废弃,不再维护。未建议替代包。

独立的验证类

v1.2 2020-01-09 10:10 UTC

This package is auto-updated.

Last update: 2022-10-15 16:53:54 UTC


README

安装

composer require randomphp/validation

如何使用

验证 $_POST|$array 数组
没有要求的键返回 true。
使用 $password 获取另一个键的值

验证通常会在返回 true 或错误消息之前,对每个输入的要求进行检查。
然而,如果你在验证之前调用 bail(),它将在第一个错误后停止。

// Without bail() active
$validation = new Validation($_POST|$array);
$validation->requirements([
  'username'        => 'required',
  'password'        => ['required', 'min:8'],
  'confirmPassword  => 'equals:$password',
  'name'            => ['optional', 'min:3']
]);
$passed = $validation->validate();
// With bail() active
$validation = new Validation($_POST|$array);
$validation->requirements([
  'username'        => 'required',
  'password'        => ['required', 'min:8'],
  'confirmPassword  => 'equals:$password',
  'name'            => ['optional', 'min:3']
]);
$validation->bail();
$passed = $validation->validate();

你可以使用 __construct() 来做一些事情或所有事情。
默认:__construct($inputs = null, $requirements = null, $validate = false, $bail = false)

$validation = new Validation($_POST|$array, $rules|[
  'username'        => 'required',
  'password'        => ['required', 'min:8'],
  'confirmPassword  => 'equals:$password',
  'name'            => ['optional', 'min:3']
]);
$passed = $validation->validate();
// Here we are telling the __construct() to validate the input by adding the true after $rules
$passed = new Validation($_POST|$array, $rules|[
  'username'        => 'required',
  'password'        => ['required', 'min:8'],
  'confirmPassword  => 'equals:$password',
  'name'            => ['optional', 'min:3']
], $validate = true);
// You can also set the __construct() to bail after first error
$passed = new Validation($_POST|$array, $rules|[
  'username'        => 'required',
  'password'        => ['required', 'min:8'],
  'confirmPassword  => 'equals:$password',
  'name'            => ['optional', 'min:3']
], $validate = true, $bail = true);

如果 optional 要求在一个输入上,它会在为空时返回 true,如果有附加到该输入的更多要求。然而,如果输入中有任何内容,其他要求将验证该值。

你也可以单独调用它们

$validation = new Validation();
$validation->isArray($array);
$validation->min($string, 12);

错误

  • 要更改 错误模式,请使用 setErrormode($boolean)。 (默认:TRUE)。
  • 错误消息仅在您想验证数组而不单独使用验证函数时才显示。

错误消息

要自定义您的消息,您可以调用以下任何一个函数

  • setMessage($rule, $message) - 这将仅更改一条消息。
  • setMessages($array) - 这将更改多条消息。

在自定义消息时可以使用一些占位符

  • :input - 将显示输入的 name 属性
  • :value - 将显示输入的值
  • :param1 - 将显示规则的第一个参数,例如 between:8|10 将显示 8
  • :param2 - 将显示规则的第二个参数,例如 between:8|10 将显示 10
  • :prefixes - 将显示 'http://', 'http://', 'ftp://'
更改消息的示例
$validation = new Validation($_POST);

// Change the error message for the rule 'isString'
$validation->setMessage('isString', "':input' must be a string."); // will show "'name' must be a string"

// Change the error message on between, alpha and date
$validation->setMessages([
  'between' => "':input' must be between :param1 and :param2", // will show "'age' must be between 13 and 25"
  'alpha' => "':value' must only alphabetic characters", // will show "'hello123' must only show alphabetic characters"
  'date' => "':value' is not a date" // will show "'hello123' is not a date"
]);

内置要求/规则

  • optional
  • isArray
  • isInteger
  • isNumeric
  • required
  • equals
  • different
  • isString
  • length
  • min
  • max
  • between
  • in
  • ip
  • ipv4
  • ipv6
  • email
  • emailDNS
  • url
  • urlActive
  • regex
  • date
  • dateFormat
  • dateBefore
  • dateAfter
  • isBoolean
  • contains
  • accepted
  • slug
  • alpha
  • alphaNum