bootpress / validator
表单验证,旨在简化与 Jörn 的 jQuery 验证插件无缝协同工作。
Requires
- php: >=5.4
- bootpress/page: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.0
- phpunit/phpunit: ^4.0
- squizlabs/php_codesniffer: ^2.5
Suggests
- bootpress/form: Coordinates form validation, errors, messages, values, and inputs in a DRY KISS way.
This package is not auto-updated.
Last update: 2024-09-14 19:53:06 UTC
README
表单验证,旨在简化与 Jörn 的 jQuery 验证插件 无缝协同工作。所有的验证程序都设计得尽可能接近 Jörn 提供的,以确保浏览器和服务器验证程序保持同步。
安装
将以下内容添加到您的 composer.json
文件中。
{ "require": { "bootpress/validator": "^1.0" } }
示例用法
<?php use BootPress\Validator\Component as Validator; $validator = new Validator($_POST);
您需要做的第一件事是为我们提供一个要验证的值数组。在这种情况下,您已提供了 $_POST
变量。现在您可以设置每个字段的规则和过滤器。
// Require a name value $validator->set('name', 'required'); // Require an email, and make sure it looks like one as well $validator->set('email', 'required|email'); // Set multiple fields at once $validator->set(array( 'password' => 'required|alphaNumeric|minLength[5]|noWhiteSpace', // Using a pipe separated string 'confirm' => array('required', 'matches[password]'), // Using an array of rules and filters )); // Set and create a custom required message for this one field $validator->set('field', array('required' => 'Do this or else.') // Change the default required error message for all fields $validator->errors['required'] = 'Why I Oughta!';
字段名称可以是数组,通过在末尾添加方括号,例如 'name[]'。它们也可以是多维数组,例如 'name[first]'、'name[players][]' 或 'name[parent][child]' 等。重要的是要记住,您必须始终在这里提供的确切名称来在其他方法中引用它们。
规则和过滤器可以是 '|'(单竖线)分隔的,或者您可以将其作为一个数组。可以通过将其作为 array($rule => $message, ...)
来指定自定义消息。参数是逗号分隔的,并放置在 '[]' 中两个方括号内。可用选项有
- 'remote[rule]' - 将
$validator->rules['rule'] = function($value){}
设置为确定提交值的有效性。函数应返回布尔值 true 或 false。 - 'default' - 如果字段为空或未设置,则为默认值。
- 'required' - 此字段必须具有值,并且不能为空。
- 'equalTo[field]' - 必须与其他表单字段中包含的相同值匹配。
- 'notEqualTo[field]' - 必须不与包含在其他表单字段中的相同值匹配。
- 数字
- 'number' - 必须是有效的十进制数,正数或负数,整数或浮点数,逗号是允许的。默认为 0。
- 'integer' - 必须是正数或负整数,没有逗号。默认为 0。
- 'digits' - 必须是正整数,没有逗号。默认为 0。
- 'min[number]' - 必须大于或等于 [number]。
- 'max[number]' - 必须小于或等于 [number]。
- 'range[min, max]' - 必须大于或等于 [min],且小于或等于 [max]。
- 字符串
- 'alphaNumeric' - 仅字母 (a-z)、数字 (0-9) 和下划线 (_) 字符。
- 'minLength[integer]' - 字符串长度必须大于或等于 [integer]。
- 'maxLength[integer]' - 字符串长度必须小于或等于 [integer]。
- 'rangeLength[minLength, maxLength]' - 字符串长度必须大于或等于 [minLength],且小于或等于 [maxLength]。
- 'minWords[integer]' - 单词数量必须大于或等于 [integer]。
- 'maxWords[integer]' - 单词数量必须小于或等于 [integer]。
- 'rangeWords[minWords, maxWords]' - 单词数量必须大于或等于 [minWords],且小于或等于 [maxWords]。
- 'pattern[regex]' - 必须匹配提供的与ECMA JavaScript兼容的正则表达式。
- 'date' - 必须是一个看起来有效的日期。不强制执行特定格式。
- 'email' - 必须是一个看起来有效的电子邮件。
- 'url' - 必须是一个看起来有效的网址。
- 'ipv4' - 必须是一个看起来有效的IPv4地址。
- 'ipv6' - 必须是一个看起来有效的IPv6地址。
- 'inList[1,2,3]' - 必须是逗号分隔的可接受值列表中的一个。
- 'noWhiteSpace' - 必须不包含任何空白。
- 过滤器
- 'singleSpace' - 移除任何重复的空白,以便在单词之间只保留单个空格。
- 'trueFalse' - 返回一个 1(true)或 0(false)整数。
- 'yesNo' - 返回 'Y' 或 'N' 值。
查看您提供给我们的 $_POST
数组是否满足所有您的需求
if ($vars = $validator->certified()) { // Process $vars } else { // The form was either not submitted, or there were errors }
返回的 $vars
都已经使用 trim()
过滤,并准备好供您按需处理。从这里开始,最好的做法是使用我们的 BootPress 表单组件,但如果您有更好的想法,则可以确定是否使用 $validator->required('field')
,获取提交的 $validator->value('field')
,检查是否有 $validator->error('field')
,获取数据规则 $validator->rules('field')
属性,以及数据消息 $validator->messages('field')
属性,找到我们分配的 $validator->id('field')
,并在创建表单和字段时设置 $validator->jquery('#form')
JavaScript。
所有上述内容只是假设您正在使用此组件验证提交的表单数据,但它同样适用于验证任何侧面的内容。我们提供的(以及我们自己使用的)静态方法包括
Validator::number(1.345); // true - this is a number Validator::number('string'); // false Validator::integer(1000); // true Validator::integer(1.345); // false - must be a whole number Validator::digits(1000); // true Validator::digits(1.345); // false - no periods allowed Validator::min(5, 3); // true - 5 is greater than 3 Validator::min(3, 5); // false - 3 is less than 5 Validator::max(5, 3); // false - 5 is greater than 3 Validator::max(3, 5); // true - 3 is less than 5 Validator::range(5, array(2, 7)); // true Validator::range(5, array(6, 7)); // false Validator::alphaNumeric('abc123'); // true Validator::alphaNumeric('abc-xyz'); // false Validator::minLength('string', 2); // true Validator::minLength('string', 7); // false Validator::maxLength('string', 7); // true Validator::maxLength('string', 2); // false Validator::rangeLength('string', array(2, 6)); // true Validator::rangeLength('string', array(7, 15)); // false Validator::rangeLength(array(1, 2), array(2, 4)); // true - there are between 2 and 4 elements in array(1, 2) Validator::rangeLength(array(1, 2), array(3, 5)); // false - 2 elements is outside the range of 3 and 5 Validator::minWords('one two three', 1); // true Validator::minWords('one two three', 5); // false Validator::maxWords('one two three', 5); // true Validator::maxWords('one two three', 1); // false Validator::rangeWords('one two three', array(1, 3)); // true Validator::rangeWords('one two three', array(0, 2)); // false // Allows phone numbers with optional country code, optional special characters and whitespace $phone_number = '/^([+]?\d{1,2}[-\s]?|)\d{3}[-\s]?\d{3}[-\s]?\d{4}$/'; Validator::pattern('907-555-0145', $phone_number); // true Validator::pattern('555-0145', $phone_number); // false Validator::date('2015-12-31'); // true Validator::date('infinite'); // false Validator::email('email@example.com'); // true Validator::email('email@example..com'); // false Validator::url('http://example.com'); // true Validator::url('example.com'); // false Validator::ipv4('175.16.254.1'); // true Validator::ipv4('2001:0db8:0000:0000:0000:ff00:0042:8329'); // false Validator::ipv6('2001:0db8:0000:0000:0000:ff00:0042:8329'); // true Validator::ipv6('175.16.254.1'); // false Validator::inList('2', array(1, 2, 3)); // true Validator::inList(7, array(1, 2, 3)); // false Validator::noWhiteSpace('whitespace'); // true Validator::noWhitespace('white space'); // false Validator::singleSpace('single space'); // 'single space' Validator::singleSpace('single space'); // 'single space' Validator::trueFalse(101)); // 1 Validator::trueFalse('true')); // 1 Validator::trueFalse('n')); // 0 Validator::trueFalse(0)); // 0 Validator::yesNo(101)); // 'Y' Validator::yesNo('true')); // 'Y' Validator::yesNo('n')); // 'N' Validator::yesNo(0)); // 'N'
您要验证的值始终排在第一位,任何参数排在第二位——以免您混淆。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。