«

yii rules验证示例总结

时间:2024-2-8 09:59     作者:韩俊     分类: PHP


yii常见字段验证示例总结。

<?php
class ContactForm extends CFormModel{
    public $id;
    public $contact;//联系人
    public $tel;//电话
    public $fax;//传真
    public $zipcode;//邮编
    public $addr;//地址
    public $mobile;//手机
    public $email;//邮箱
    public $website;//网址
    public $qq;//QQ
    public $msn;//MSN
    public $add_time;//添加时间
    public function rules(){
        return array(
            array('id,add_time','numerical','integerOnly'=>true),//这些字段必须为数字
            //关于safe的理解可参考:http://www.maopiaopiao.com/php-template-framework/1163.html
            array('contact,tel,fax,zipcode,addr,mobile,email,website,qq,msn,add_time','safe')
            array('contact','required','on'=>'edit','message'=>'联系人必须填写.'),
            array('contact','length','on'=>'edit','min'=>2,'max'=>10,'tooShort'=>'联系人长度请控制在2-10个字符.','tooLong'=>'联系人长度请控制在2-10个字符.'),
            array('tel','match','pattern'=>'/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message'=>'请输入正确的电话号码.'),
            array('fax','match','pattern'=>'/^(\d{3}-|\d{4}-)(\d{8}|\d{7})?$/','message'=>'请输入正确的传真号码.'),
            array('mobile','match','pattern'=>'/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/','message'=>'请输入正确的手机号码.'),
            array('email','email','on'=>'edit','message'=>'邮箱输入有误.'),
            array('zipcode','required','on'=>'edit','message'=>'邮编必须填写.'),
            array('zipcode','numerical','on'=>'edit','message'=>'邮编是6位数字.'),
            array('zipcode','length','on'=>'edit','min'=>6,'max'=>6,'tooShort'=>'邮编长度为6位数.','tooLong'=>'邮编长度为6位数.'),
            array('website','url','on'=>'edit','message'=>'网址输入有误.'),
            array('qq','match','pattern'=>'/^[1-9]{1}[0-9]{4,11}$/','message'=>'请输入正确的QQ号码.'),
            array('msn','email','on'=>'edit','message'=>'MSN输入有误.'),
        );
    }
}

标签: php php教程

热门推荐