表单验证

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

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

简单示例:

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 = '')方法获取

数组方式

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

Last updated

Was this helpful?