目录
当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Validation 方法 - 类別

优质
小牛编辑
118浏览
2023-12-01

类别帮助你验证使用者输入,如果你想要同时建立一个表单&它的验证, 使用 Fieldset 类别代替。

类别方法

forge($fieldset = 'default')

forge 方法回传一个新的 Validation 实例。 并用一个 $fieldset 与 Fieldset 连结。

静态
参数
参数类型预设描述
$fieldset字串|Fieldset
'default'
名称或要连结的 Fieldset 实例。
回传Validation 物件
抛出\DomainException,当 Fieldset 名称或实例已经有一个关联的 Validation 实例。
範例
// 建立一个新的 Validation 实例
$val = Validation::forge();

// 关联新的 Validation 实例与一个Fieldset,使用 Fieldset 名称
$val = Validation::forge('my_fieldset');

// 关联新的 Validation 实例与一个Fieldset,透过传递 Fieldset 实例
$fieldset = Fieldset::instance('my_fieldset');
$val = Validation::forge($fieldset);

instance($name = null)

instance 方法回传与 Vaildation 实例关联的 Fieldset 实例的识别符 $name, 或预设的 Fieldset(如果需要的话会建立)。

静态
参数
参数类型预设描述
$name字串
null
你想回传的 Validation 的 Fieldset 名称。
回传Validation 物件
false 如果指定 Fieldset 实例不存在
範例
// 取得预设 Fieldset 的 Validation 实例
$val = Validation::instance();

// 取得一个特定 Fieldset 的 Validation 实例
$val = Validation::instance('my_fieldset');

active()

active 方法回传目前活跃的 Validation 实例。

静态
参数
回传Validation - 目前活跃的 Validation 实例
範例
// 取得目前活跃的 Validation 实例
$val = Validation::active();

active_field()

active_field 方法回传目前被验证的栏位。

静态
参数
回传Fieldset_Field - 目前被验证的栏位
範例
// 取得目前被验证的栏位
$field = Validation::active_field();

fieldset()

fieldset 方法回传关联的 Fieldset。

静态
参数
回传Fieldset - 关联的 Fieldset
範例
// 取得关联的 Fieldset
$val = Validation::forge('my_fieldset');
$fieldset = $val->fieldset();

add_field($name, $label, $rules)

add_field 方法是 add() 方法一个更简单的别名,它可以在一个步骤指定栏位名称、标籤以及规则。

静态
参数
参数类型预设描述
$name字串
要指派验证规则的栏位名称。
$label字串
栏位的标籤。
$rules混合
带有群组在方括号内的规则参数(选择性)的验证规则组, 并用逗号分隔。在被用垂直线或管道符号(|)的规则可被传递做为一个字串, 或做为一个规则阵列。
回传Fieldset_Field
範例
// 在不同类型的栏位设定规则
$val->add_field('username', 'Username', 'required|trim|valid_string[alpha,lowercase,numeric]');
$val->add_field('email', 'Email', 'required|trim|valid_email');
$val->add_field('age', 'Age', 'valid_string[numeric]');

// 做为一个阵列
$val->add_field('username', 'Username', array('required', 'trim', 'valid_string[alpha,lowercase,numeric]'));

set_message($rule, $message)

set_message 方法为此 Validation 实例覆写语言档案讯息。当没有讯息被指派到你正在使用的规则时,它也是有用的。

静态
参数
参数类型预设描述
$rule字串
要指派此讯息的规则名称。
$message字串|null
要为此规则显示的讯息,如果传递一个字串,或 null 来回复到语言档案。
回传Validation 实例
範例
// 覆写 required 规则讯息
$val->set_message('required', 'You have to fill in your :label so you can proceed');
// ……完成时,设回预设讯息
$val->set_message('required', null);
// 给你建立自订规则的讯息
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');

get_message($rule)

get_message 方法为此 Validation 实例取回一个指定的错误讯息。只有透过 set_message 方法设定的讯息会被回传。

静态
参数
参数类型预设描述
$rule字串
你需要取得讯息的规则名称。
回传字串
範例
// 覆写需要的规则讯息
$val->set_message('custom_rule', 'The :label you entered is previously registered. Please choose another one');
// ……之后,或在你程式码中的其他地方,取得此讯息
$val->get_message('required', null);

add_callable($class)

add_callable 方法添加一组自订或扩充的验证验证规则。你不须要写一个完整的回呼(callback),只要一个类别名称的字串。这也允许从此物件覆写功能,因为新类别是附加在前的。

静态
参数
参数类型预设描述
$class字串|物件
做为字串的类别名称,或物件名称
回传Validation 实例
範例

$val->add_callable('myvalidation');

为了完整使用此方法的範例,检查扩充 Validation 类别,以及所需要的类别。

remove_callable($class)

remove_callable 方法从可呼叫阵列移除一个可呼叫。

静态
参数
参数类型预设描述
$class字串|物件
做为字串的类别名称,或物件名称
回传Validation 实例
範例
$val->remove_callable('myvalidation');

callables()

callables 方法取回你不需要添加一个完整回呼(callback)但只要方法名称的物件

静态
参数
回传阵列

run($input = null, $allow_partial = false, $temp_callables = array())

针对传递的 input 运行定义的验证规则,或 Input::post() 如果没传东西的话。

静态
参数
参数类型预设描述
$input阵列
null
input 栏位和值的关联阵列
$allow_partial布林
false
如果为 false,全部有定义验证规则的栏位必须在 input 出现
$temp_callables阵列
array()
Any additional callables required to run all defined validation rules
回传布林,如果验证过为 true,如果失败为 false
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ( ! $val->run())
{
	foreach ($val->error() as $field => $error)
	{
		echo $error->get_message();
		// Title 栏位是必要且必须包含一个值。
	}
}

input($key = null, $default = null)

Returns one or all input values, or the default value if the requested key is not present in the input.

静态
参数
参数类型预设描述
$key字串
null
Name of the input field, or null to return all input
$default混合
null
Value to return if the key is not present in the input
回传Mixed, the input value or the value specified as $default
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ($val->run())
{
	// if something was posted, echo it
	echo $val->input('title');
}

Input is only available after the validation rules have run. By default, input() will fall back to Input::param() when no rules have run. This behaviour can be controlled through the validation config file.

validated($field = null, $default = false)

Returns one or all validated input values, or the default value if the requested key is not present in the validated input.

静态
参数
参数类型预设描述
$field字串
null
Name of the input field, or null to return all input
$default混合
null
Value to return if the key is not present in the input
回传Mixed, the validated input value or the value specified as $default
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ($val->run())
{
	// if something was posted, echo it
	echo $val->validated('title');
}

Where input() returns the raw input value, validated() will return the validated value, which might be different than the input because some validation rules alter the input (for example a rule like strtoupper).

error($field = null, $default = false)

error 方法回传在验证期间发生的指定或所有错误。

静态
参数
参数类型预设描述
$field字串
null
你想要回传的 Validation_Error 实例的栏位名称。如果你没指定,会回传所有错误物件。
$default混合
false
如果指定栏位的错误不存在,回传这个值。
回传Validation_Error 实例
阵列 如果你没指定栏位名称
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ( ! $val->run(array()))
{
	foreach ($val->error() as $field => $error)
	{
		echo $error->get_message();
		// Title 栏位是 required 的并且必须包含一个值。
	}
}

error_message($field = null, $default = false)

error_message 方法回传在验证期间发生的指定或所有错误讯息。

静态
参数
参数类型预设描述
$field字串
null
你想要回传的错误讯息的栏位名称。如果你没指定,会回传所有错误讯息。
$default字串
false
如果指定栏位的错误不存在,回传这个值。
回传字串
阵列 如果你没指定栏位名称
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ( ! $val->run(array()))
{
	foreach ($val->error_message() as $field => $message)
	{
		echo $message;
		// Title 栏位是 required 的并且必须包含一个值。
	}
}

show_errors($options = array())

show_errors 根据定义的样板回传呈现在 HTML 中的验证错误列表。

静态
参数
参数类型预设描述
$options阵列
array()
HTML 样板的选项,以覆写来自验证配置档案的预设值
回传字串,呈现的 HTML
範例
$val = Validation::forge('article');
$val->add_field('title', 'Title', 'required');

if ( ! $val->run(array()))
{
	echo $val->show_errors();
}

以下选项可以被传递:'open_list'、'close_list'、'open_error'、'close_error' 和 'no_errors'。 预设情况下会产生一个无序清单,且如果没找到验证错误会回传一个空字串。

add($name, $label = '', array $attributes = array(), array $rules = array())

Fieldset add() 方法的别名。

add_model($class, $instance = null, $method = 'set_form_fields')

Fieldset add_model() 方法的别名。