首页 > 其他 > 详细

Phalcon框架之———— 2.0升级到3.0 问题Model验证问题解决

时间:2017-01-11 10:19:04      阅读:215      评论:0      收藏:0      [点我收藏+]

Github源码:https://github.com/phalcon/cphalcon/tree/master/phalcon/validation/validator

Phalcon 2.0 Model 字段验证:

public function validation()
    {
        $this->validate(new PresenceOf(array(
            "field" => ‘code‘,
            "message" => ‘应用名称不能为空‘,
        )));
        $this->validate(new Regex(array(
            "field" => ‘code‘,
            "message" => ‘应用名称只能包含字母、数字、 中、下划线‘,
            ‘pattern‘ => ‘/^[-A-Za-z0-9_]+$/‘,
        )));
        $this->validate(new Model\Validator\Uniqueness(array(
            ‘field‘ => ‘code‘,
            ‘message‘ => ‘应用名称已被使用‘
        )));
        $this->validate(new PresenceOf(array(
            "field" => ‘name‘,
            "message" => ‘名称不能为空‘,
        )));
        $this->validate(new PresenceOf(array(
            "field" => ‘mechanismId‘,
            "message" => ‘请选择归属推理系统‘,
        )));

        return $this->validationHasFailed() != true;
    }

Phalcon 3.0 Model 字段验证:

 public function validation()
    {
        $validator = new Validation();
        //验证值不为null或空字符串
        $validator->add(
            [
                ‘mobile‘,
                ‘password‘
            ],
            new PresenceOf([
                "message" => [
                    ‘mobile‘ => ‘手机号不能为空‘,
                    ‘password‘ => ‘密码不能为空‘,
                ],
            ])
        );
        //允许验证字段的值是否与正则表达式匹配
        $validator->add(
            [
                ‘mobile‘,
                ‘password‘
            ],
            new RegexValidator([
                ‘pattern‘ => [
                    ‘mobile‘ => "/^(13[0-9]|14[0-9]|17[0-9]|15[0-9]|18[0-9])\d{8}$/",
                    ‘password‘ => "/^[A-Za-z0-9_]+$/",
                ],
                ‘message‘ => [
                    ‘mobile‘ => ‘手机号只能包含英文、数字和下划线‘,
                    ‘password‘ => ‘密码只能包含英文、数字和下划线‘
                ]
            ])
        );
        //检查相关表中的字段是否唯一
        $validator->add(
            ‘mobile‘,
            new Uniqueness([
                ‘model‘ => ‘User‘,
                ‘message‘ => ‘:field must be unique‘
            ])
        );
        return $this->validate($validator);
    }

 

Phalcon框架之———— 2.0升级到3.0 问题Model验证问题解决

原文:http://www.cnblogs.com/tinywan/p/6272428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!