本文實例講述了yii用戶注冊表單驗證實現方法。分享給大家供大家參考,具體如下:
視圖層:register.php
<?php //使用小物件生成form元素 $form=$this->beginWidget('CActiveForm'); ?> <!--用戶名--> <?php echo $form->labelEx($model,'username');?> <?php echo $form->textField($model,'username');?> <?php echo $form->error($model,'username');?> <br> <!--密碼--> <?php echo $form->labelEx($model,'password');?> <?php echo $form->passwordField($model,'password');?> <?php echo $form->error($model,'password');?> <br> <!--確認密碼--> <?php echo $form->labelEx($model,'password2');?> <?php echo $form->passwordField($model,'password2');?> <?php echo $form->error($model,'password2');?> <br> <!--郵箱--> <?php echo $form->labelEx($model,'email');?> <?php echo $form->textField($model,'email');?> <?php echo $form->error($model,'email');?> <br> <!--性別--> <?php echo $form->labelEx($model,'sex');?> <?php echo $form->radioButtonList($model,'sex',array(1=>'男',2=>'女'),array("separator"=>" "));?> <br> <!--年級--> <?php echo $form->labelEx($model,'grade');?> <?php echo $form->dropDownList($model,'grade',array(0=>"--請選擇--",1=>"2010級",2=>"2011級",3=>"2012級"));?> <?php echo $form->error($model,'grade');?> <br> <?php echo $form->labelEx($model,'hobby');?> <?php echo $form->checkBoxList($model,'hobby',array(1=>"籃球",2=>"足球",3=>"排球"),array("separator"=>" "));?> <?php echo $form->error($model,'hobby');?> <!--提交--> <?php echo CHtml::submitButton('提交');?> <?php $this->endWidget(); ?>
model層的代碼如下:User.php
<?php /* * To change this template, choose Tools | Templates * and open the template in the editor. */ class User extends CActiveRecord{ //創建一個模型的對象 靜態 public $password2;//非數據庫的字段,但是在view中需要用到 public static function model($className = __CLASS__) { return parent::model($className); } //返回當前數據表的名字 public function tableName() { //parent::tableName(); return '{{user}}';//這樣寫可以省略前綴 } //設置標簽的顯示名字 public function attributeLabels() { return array( "username"=>"姓名", "password"=>"密碼", "password2"=>"確認密碼", "email"=>"郵箱", "sex"=>"性別", "grade"=>"年級", "hobby"=>"愛好", ); } //驗證表單域 public function rules() { return array( array("username","required","message"=>"用戶名不能為空"), array("password","required","message"=>"密碼不能為空"), //驗證密碼和確認密碼 array("password2","compare","compareAttribute"=>"password","message"=>"兩次密碼不一致"), //驗證郵箱 array("email","email","allowEmpty"=>false,"message"=>"郵箱格式不正確"), //驗證年級 array("grade","in","range"=>array(1,2,3),"message"=>"請選擇年級"), //驗證愛好,使用自定義規則 array("hobby","checkHobby"), ); } //驗證愛好 function checkHobby(){ $this->hobby; $len= strlen($this->hobby); if($len<3){ $this->addError("hobby","愛好至少為2個以上"); } } } ?>
control層的代碼如下:
//信息添加 添加顯示頁面和添加處理使用同一個方法 public function actionAdd(){ $user=new User(); if(isset($_POST['User'])){ // foreach ($_POST['User'] as $k =>$v){ // $user->$k=$v; // } if(is_array($_POST['User']['hobby'])) $_POST['User']['hobby']= implode(",", $_POST['User']['hobby']); //print_r($_POST['User']); $user->attributes=$_POST['User']; if($user->save()){ echo 'success'; }else{ echo 'error'; } } $this->renderPartial("new",array("model"=>$user)); }
其中,表單的自動驗證,在model層實現,表單元素的名稱與數據庫中的字段名字一致,可設置錯誤信息進行提示。
希望本文所述對大家基於yii框架的PHP程序設計有所幫助。