zoukankan      html  css  js  c++  java
  • YII 1.0模型标签与验证规则,前后台验证

    model Admin.php

    model(),tabName()是固定格式

    <?php
    /*  管理员模型
     * --------------------------------------------
     * @auther haoxiang
     * --------------------------------------------
     * @time 2015/8/3
     */
    class Admin extends CActiveRecord{
    	public $password1;
    	public $password2;
        public static function model($className = __CLASS__){
            return parent::model($className);
        }
        public function tableName(){
            return "{{admin}}";
        }
        /*
        标签名字
         */
        public function attributeLabels(){
    		return array(
    			'password' => '原始密码',
    			'password1' => '新密码',
    			'password2' => '确认密码'
    		);
    	}
    	/*
    	 * 验证规则
    	 * required 的字段就会有小星号表示必须
    	 */
    	public function rules(){
    		return array(
    			array('password','required', 'message'=>'原始密码必填'),
    			array('password1','required', 'message'=>'新密码必填'),
    			array('password2','required', 'message'=>'确认密码必填'),
                array('password2','compare', 'compareAttribute'=>'password1','message'=>'两次密码不一致'),
                array('password','check_password'),
    		);
    	}
        /*
         * 自定义验证规则
         */
        public function check_password(){
            $user = Yii::app()->user->name;
            $userinfo = Admin::model()->find('username = :name',array(':name'=>$user));
            if(md5($this->password) != $userinfo['password']){
                $this->addError('password','用户名或密码错误');
            }
        }
    }
    

     在模板中的使用

    <?php $form=$this->beginWidget('CActiveForm', array('enableClientValidation'=>true, 'clientOptions'=>array('validateOnSubmit'=>true))); ?>
    	<table class="table">
    		<tr>
    			<td class="th" colspan="10">修改密码</td>
    		</tr>
    		<tr>
    			<td>用户</td>
    			<td><?php echo Yii::app()->user->name ?></td>
    		</tr>
    		<tr>
    			<td><?php echo $form->labelEx($Admin, 'password') ?></td>
    			<td>
    				<?php echo $form->passwordField($Admin, 'password') ?>
    				<?php echo $form->error($Admin, 'password') ?>
    			</td>
    		</tr>
    		<tr>
    			<td><?php echo $form->labelEx($Admin, 'password1') ?></td>
    			<td>
    				<?php echo $form->passwordField($Admin, 'password1') ?>
    				<?php echo $form->error($Admin, 'password1') ?>
    			</td>
    		</tr>
    		<tr>
    			<td><?php echo $form->labelEx($Admin, 'password2') ?></td>
    			<td>
    				<?php echo $form->passwordField($Admin, 'password2') ?>
    				<?php echo $form->error($Admin, 'password2') ?>
    			</td>
    		</tr>
    		<tr>
    			<td colspan="10">
    				<input type="submit" class="input_button" value="修改" />
    			</td>
    		</tr>
    	</table>
    	<?php $this->endWidget() ?>
    

     控制器

    <?php
    /*
    后台用户模块
     */
    class UserController extends Controller{
        public function actionPasswd(){
        	$Admin = new Admin();  //加载模型
            if(isset($_POST['Admin'])){
                $Admin->attributes = $_POST['Admin'];
                if($Admin->validate()){
                    //$Admin->addError('password','未知错误');
                    $userinfo = $Admin->model()->find('username = :name',array(':name'=>Yii::app()->user->name));
    
                    $password = md5($_POST['Admin']['password1']);
                    $result = $Admin->model()->updateByPk($userinfo->id,array('password'=>$password));
                    if($result){
                        Yii::app()->user->setFlash('success','修改成功');
                    }
                }
            }
            $this->render('passwd',array('Admin'=>$Admin));
        }
    }
    

    开启前台异步验证

    <?php $form=$this->beginWidget('CActiveForm', array('enableClientValidation'=>true, 'clientOptions'=>array('validateOnSubmit'=>true))); ?>
    

     规则列表

    • boolean:  确保特性有一个布尔值。
    • captcha: 确保特性值等于 CAPTCHA 中显示的验证码。
    • compare:  确保特性等于另一个特性或常量。
    • email: 确保特性是一个有效的Email地址。
    • default: 指定特性的默认值。
    • exist:  确保特性值可以在指定表的列中可以找到。
    • file: 确保特性含有一个上传文件的名字。
    • filter : 通过一个过滤器改变此特性。
    • in:  确保数据在一个预先指定的值的范围之内。
    • lengths 确保数据的长度在一个指定的范围之内。
    • match: 确保数据可以匹配一个正则表达式。
    • numerical:  确保数据是一个有效的数字。
    • required:  确保特性不为空。
    • type: 确保特性是指定的数据类型。
    • unique:  确保数据在数据表的列中是唯一的。
    • url:  确保数据是一个有效的 URL。

    public function rules()
    {
      return array(
       array(‘username’, ‘required’),
       array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12),
       array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’),
       array(‘password’, ‘authenticate’, ‘on’=>’login’),array(‘Price’,’numerical’, ‘integerOnly’=>true),
          array(‘title, content, status’, ‘required’),
          array(‘title’, ‘length’, ‘max’=>128),
          array(‘status’, ‘in’, ‘range’=>array(1,2,3)),
          array(‘tags’, ‘match’, ‘pattern’=>’/^[ws,]+$/’,‘message’=>’Tags can only contain word characters.’),
          array(‘tags’, ‘normalizeTags’),
          array(‘title, status’, ‘safe’, ‘on’=>’search’),
      );
    }
  • 相关阅读:
    msmms (二) sms与mms 简述!
    msmms (一) sms与mms区别
    RTSP RTSP(Real Time Streaming Protocol),RFC2326,实时流传输协议,是TCP/IP协议体系中的一个应用层协议
    GPRS GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可用的一种移动数据业务,属于第二代移动通信中的数据传输技术
    CrtCtl (客户端认证的证书、私钥)的控制
    ID
    Pb (数据存储单位)
    PDP 有多种定义,具体哪一种还需研究!!!!
    CNN 美国有线电视新闻网 wapCNN WAP 指无线应用通讯协议 ---- 美国有线电视新闻网 的无线应用
    CMWAP CMWAP是手机上网使用的接入点的名称
  • 原文地址:https://www.cnblogs.com/mr-amazing/p/4741746.html
Copyright © 2011-2022 走看看