hypejunction/hypeprototypervalidators

此包已被废弃且不再维护。未建议替换包。

hypePrototyper 验证规则

4.2.2 2015-09-02 19:42 UTC

This package is auto-updated.

Last update: 2020-01-29 03:22:51 UTC


README

预定义验证器

type

断言用户输入应为特定类型。

  • string
  • alnum - 允许字母数字字符和空格
  • alpha - 允许字母字符和空格
  • int - 只允许整数值
  • numeric - 允许数值
  • date - 允许日期/时间字符串和对象
  • url - 只允许有效的URL
  • email - 只允许有效的电子邮件地址
	$field = array(
		'type' => 'text',
		'validation_rules' => array(
			'type' => 'alnum',
		),
	);

minmax

断言用户输入介于 min 和 max 值之间

	$field = array(
		'type' => 'text',
		'validation_rules' => array(
			'type' => 'int',
			'min' => 10,
			'max' => 20,
		),
	);

minlengthmaxlength

断言用户输入的长度介于 min 和 max 值之间

	$field = array(
		'type' => 'password',
		'validation_rules' => array(
			'type' => 'string',
			'minlength' => 6,
			'maxlength' => 25,
		),
	);

contains

断言用户输入包含预定义的字符串

	$field = array(
		'type' => 'text',
		'validation_rules' => array(
			'type' => 'string',
			'contains' => 'hello world',
		),
	);

regex

断言用户输入匹配正则表达式模式

	$field = array(
		'type' => 'time',
		'validation_rules' => array(
			'type' => 'string',
			'regex' => '^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$',
		),
	);

自定义验证器

您可以使用以下模式定义自定义验证规则


// Callback for validating user input
elgg_register_plugin_hook_handler('validate:my_rule', 'prototyper', 'my_callback');

// Register the validation rule to make it available in hypePrototyperUI
hypePrototyper()->config->registerValidationRule('my_rule');

客户端验证

通过 Parsley.js 提供部分客户端验证。请启用验证,并将 data-parsley-validate 添加到表单属性中。


echo elgg_view_form('my_prototyped_form', array(
	'enctype' => 'multipart/form-data',
	'data-parsley-validate' => true,
), $vars);