jakesutherland/nomad-validate

WordPress PHP Composer 包,提供了一种简单的方式来验证数据是否符合一系列规则并生成错误消息。

1.1.0 2021-08-26 01:21 UTC

This package is auto-updated.

Last update: 2024-09-26 07:57:27 UTC


README

WordPress PHP Composer 包,提供了一种简单的方式来验证数据是否符合一系列规则并生成错误消息。

安装

您可以通过 composer 在项目中安装 Nomad Validate。

$ composer require jakesutherland/nomad-validate

依赖关系

Nomad Validate 依赖于 Nomad Helpers 以提供各种函数和实用工具,并且需要安装才能运行。

如果您出于某种原因没有通过 composer 将 Nomad Validate 作为项目中的必需包安装,您仍然需要运行 composer install 来安装其依赖项,因为它们不包括在仓库中。

文档

Nomad Validate 有一个简单的函数,您可以使用它来验证数据是否符合一系列规则。

示例用法

每个数据项必须具有以下参数: keylabelvaluerules

您可以通过 error_messages 参数可选地覆盖规则错误消息,并指定规则键和您自定义的消息。您可以在下面的 terms 示例中看到这一点。

<?php

use function Nomad\Validate\nomad_validate;

$validated = nomad_validate( array(
	'first_name' => array(
		'key'   => 'first_name',
		'label' => 'First Name',
		'value' => $_POST['first_name'],
		'rules' => array( 'required', 'minlength:2', 'maxlength:20' ),
	),
	'last_name' => array(
		'key'   => 'last_name',
		'label' => 'Last Name',
		'value' => $_POST['last_name'],
		'rules' => array( 'required', 'minlength:2', 'maxlength:50' ),
	),
	'email_address' => array(
		'key'   => 'email_address',
		'label' => 'Email Address',
		'value' => $_POST['email_address'],
		'rules' => array( 'required', 'email' ),
	),
	'terms' => array(
		'key'            => 'terms',
		'label'          => 'Terms and Conditions',
		'value'          => $_POST['terms'],
		'rules'          => array( 'required' ),
		'error_messages' => array(
			'required' => 'You must agree to the terms and conditions.',
		),
	),
) );

if ( $validated->is_valid() ) {
	// Do something if everything is valid.
} else {
	// Do something if there was an error. Display error messages.
	$messages = $validated->get_all_error_messages();
}

可用规则

每个规则都是单独添加到每个数据项的 rules 参数中。

一些规则包含参数,允许您进一步自定义如何比较和验证数据。要传递参数,请在您的规则键后添加冒号 :,然后提供参数值(《ruleargument》)。您可以在下面的规则示例中看到。

boolean

用法: booleanboolean:strictboolean:truthyboolean:falsey

描述:检查值是否为布尔值(《true”或“false”)。您还可以检查“truthy”(true、1、'true'、'1'、'yes'、'on'、'truthy')或“falsey”(false、0、'false'、'0'、'no'、'off'、'falsey')值。

choices

用法: choices:value1,value2,value3

描述:检查值是否是提供的选项之一。每个选项由逗号分隔。

date

用法: date:Y-m-d

描述:检查值是否是指定格式的日期。有关 DateTime 格式选项,请参阅 https://php.ac.cn/manual/en/datetime.format.php

email

用法: email

描述:检查值是否是有效的电子邮件地址。

equals

用法: equals:value

描述:检查值是否等于提供的值。

letters

用法: letters

描述:检查值是否只包含字母。

matches

用法: matches:my_field_key

描述:检查值是否与指定键的值匹配。

maxlength

用法: maxlength:20

描述:检查值字符长度是否小于或等于提供的数字。

max

用法: max:2021

描述:检查值是否小于或等于提供的数字。

minlength

用法: minlength:10

描述:检查值字符长度是否大于或等于提供的数字。

min

用法: min:1900

描述:检查值是否大于或等于提供的数字。

numbers

用法: numbers

描述:检查值是否只包含数字。

numeric

用法: numeric

描述:检查值是否为数值。

regex

用法: regex:/^([A-Za-z0-9_\-\.]*)$/

描述:检查值是否与给定的正则表达式匹配。

必需

用法:必需

描述:检查值是否不为空。

trim

用法:trim

描述:这是一个特殊规则,仅从值的左右两边删除空白字符。

url

用法:url

描述:检查值是否是有效的URL。

可用钩子

nomad/validate/{$key}

/**
 * Filters the validity of a specific key.
 *
 * Gives you an opportunity to change whether or not a specific key is valid.
 *
 * @since 1.0.0
 *
 * @param boolean $valid Whether or not the specific key is valid.
 * @param string  $key   The data key.
 * @param string  $label Label used in error messages.
 * @param mixed   $value The data value.
 * @param array   $rules Rules to process the value against.
 */
$valid = apply_filters( "nomad/validate/{$key}", $valid, $key, $label, $value, $rules );

nomad/validate/{$key}/{$rule_key}/error_message

/**
 * Filter the error message for a specific data key and rule key.
 *
 * The filter is available if you want to use it, but it is easier
 * to pass in a custom message through the `error_messages`
 * argument when building your initial data array.
 *
 * @since 1.0.0
 *
 * @param string $error_message The error message that was generated.
 * @param string $key           The data key.
 * @param string $label         Label used in error messages.
 * @param mixed  $value         The data value.
 * @param string $rule_key      Rule key being processed.
 * @param string $argument      Argument for the rule being processed.
 */
$error_message = apply_filters( "nomad/validate/{$key}/{$rule_key}/error_message", $error_message, $key, $label, $value, $rule_key, $argument );

nomad/validate/error_messages

/**
 * Filter all error messages generated during validation, listed by data key.
 *
 * @since 1.0.0
 *
 * @param array Error messages generated during validation, listed by data key.
 */
$error_messages = apply_filters( 'nomad/validate/error_messages', $error_messages );

变更日志

v1.1.0

  • 更新了 Nomad_Validate 类的文件名,使其以 class- 为前缀。
  • 更新了 Nomad_Validatefinal 类。
  • 更新了规则 minnumberminmaxnumbermax。两者仍然向后兼容。
  • 更新了 choices 规则的错误信息措辞。
  • 将依赖项 NomadHelpers 更新到最新版本(1.2.0)。这引入了 register_nomad_package() 函数,现在它被用来注册 Nomad Validate 包。
  • 修复了 choices 规则,使其不再将值视为必需。

v1.0.0

  • 首次发布

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件

版权

版权(c)2021 Jake Sutherland