> For the complete documentation index, see [llms.txt](https://doc.kaliphp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.kaliphp.com/forms.md).

# 表单验证

框架提供了一套完整的表单验证解决方案，适用于绝大多数场景。

&#x20;表单验证支持所有类型的验证以及自定义方法、自定义错误信息。&#x20;

### 简单示例：

```
namespace control;
class ctl_user extends ctl_base
{
    // 自定义验证方法
    public function username_check()
    {
        // 设置验证规则
        $val = cls_validate::instance()
            ->set_rules('username', 'Username', 'required|minlength[5]|maxlength[12]')
            ->set_rules('password', 'Password', 'required|minlength[8]')
            ->set_rules('passconf', 'Password Confirmation', 'required|matches[password]')
            ->set_rules('email', 'Email', 'required|email');

        // 运行验证程序。成功返回 TRUE，失败返回 FALSE
        if ($val->run() == FALSE)
        {
            tpl::display('myform');
        }
        else
        {
            tpl::display('formsuccess');
        }

    }
}
```

### 验证类型

系统提供了21种默认验证方式，验证失败时都会记录错误信息，用户可以通过`error($field = '', $prefix = '', $suffix = '')`方法获取

```
required 必选字段
remote 请修正该字段
email 请输入正确格式的电子邮件
url 请输入合法的网址
date 请输入合法的日期
numeric 数字类型，包括整型、浮点型
integer 整型类型，包括正数，负数
decimal 只能输入小数
idcard 请输入合法的身份证号
creditcard 请输入合法的信用卡号
matches[param] 请再次输入相同的值
accept 请输入拥有合法后缀名的字符串
maxlength[param] 长度不能大于 {param} 位
minlength[param] 长度不能小于 {param} 位
exactlength[param] 长度只能等于 {param} 位
rangelength[minlen:maxlen] 长度介于 {minlen} 和 {maxlen} 之间
max[param] 请输入一个最大为 {param} 的值
min[param] 请输入一个最小为 {param} 的值
range[minnum:maxnum] 请输入一个介于 {minnum} 和 {maxnum} 之间的值
```

### 数组方式

验证类除了迭代方式，还支持使用数组来设置验证规则

```
$config = array(
    array(
        'field' => 'username',
        'label' => 'Username',
        'rules' => 'required'
    ),
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'required',
        'errors' => array(
            'required' => 'You must provide a password.',
        ),
    ),
    array(
        'field' => 'passconf',
        'label' => 'Password Confirmation',
        'rules' => 'required'
    ),
    array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'required'
    )
);
cls_validate::instance()->set_rules($config);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.kaliphp.com/forms.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
