2012-07-26 38 views
1

狀態:我有3個依賴用戶/創建下拉自定義提交驗證。他們正在驗證罰款。 問:提交後,如果沒有通過驗證,我所有的下拉列表都是空白的。我能怎麼做?yii自定義下拉菜單驗證後空白

這是提交 before submitting

之前,這是提交 After submitting

後,這是部門TBL結構

department 
id | name  | p_id | company_id 
1 | dep1  | 0 | 1 
2 | dep2  | 0 | 1 
3 | sec1.1 | 1 | 1 
4 | sec2.1 | 2 | 1 
5 | team1.1.1 | 3 | 1 
6 | team1.1.2 | 3 | 1 
7 | team2.1.1 | 4 | 1 

這是用戶TBL結構

user 
id | name | company_id | team_id 

team_id與部門tbl的關係。

這是在用戶模式

public $section; 
    public $team; 
public function rules() 
    { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 
     return array(
      array('login_name, email, rank_id, company_id, team_id, section, team', 'required'), // team_id is Department 
      array('rank_id, company_id, team_id', 'numerical', 'integerOnly'=>true), 
      array('login_name, first_name, last_name', 'length', 'max'=>100), 
      array('password', 'length', 'max'=>50), 
      array('email', 'length', 'max'=>200), 

      array('password', 'compare', 'on' => 'create'), 
      array('password_repeat', 'safe', 'on' => 'create'), 
      array('password, password_repeat', 'required', 'on' => 'create'), 

      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('first_name, last_name, email, created, updated, rank_id, company_id, team_id', 'safe', 'on'=>'search'), 
     ); 
    } 

這種觀點(_form.php這個)

<!-- This is Department --> 
<div class="row"> 
     <?php 
      if($model->company_id) 
      { 
       $records = Department::model()->findAll('p_id=0 AND company_id=:company_id', array(':company_id'=>(int) $model->company_id)); 
       $department_list = CHtml::listData($records, 'id', 'name'); 
      }else $department_list = array(); 
     ?> 
     <?php echo $form->labelEx($model,'team_id'); ?> 
     <?php echo $form->dropDownList($model,'team_id', $department_list, array('prompt'=>'Please select a department', 
       'ajax' => array(
        'type'=>'POST', //request type 
        'url'=>CController::createUrl('user/dynamicsection'), //url to call. 
        'dataType'=>'json', 
        'data'=>array('department_id'=>'js:this.value'), 
        'success'=>'function(data) { 
         $("#User_section").html(data.ddsection); 
         $("#User_team").html(data.ddteam); 
        }', 
       ) 
      ) 
     ); 
     ?> 
     <?php echo $form->error($model,'team_id'); ?> 
    </div> 
<!-- This is section --> 
    <div class="row"> 
     <?php echo $form->labelEx($model,'section'); ?> 
     <?php echo $form->dropDownList($model,'section', array(), array('prompt'=>'Please select a section', 
     'ajax' => array(
      'type'=>'POST', //request type 
      'url'=>CController::createUrl('user/dynamicteam'), //url to call. 
      'dataType'=>'json', 
      'data'=>array('section'=>'js:this.value'), 
      'success'=>'function(data) { 
         $("#User_team").html(data.ddteam); 
        }', 
     ))); ?> 

     <?php echo $form->error($model,'section'); ?> 


    </div> 
<!-- This is Team --> 
    <div class="row"> 
     <?php echo $form->labelEx($model,'team'); ?> 
     <?php echo $form->dropDownList($model,'team', array(), array('prompt'=>'Please select a team')); ?> 
     <?php echo $form->error($model,'team'); ?> 


    </div> 

該控制器

public function actionCreate() 
    { 
     $model=new User; 

     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['User'])) 
     { 
      if(User::model()->exists("login_name='".$_POST['User']['login_name']."'")) 
      { 
       $_SESSION['User'] = $_POST['User']; 

       $duplicate_error = '<p>Please fix the following input errors:</p> 
        <ul> 
        <li>Name is duplicated.</li> 
        </ul>'; 
       Yii::app()->user->setFlash('duplicate_error',$duplicate_error); 
       $this->redirect(array('user/create')); 
      }else{ 
       $currentTime = date('Y-m-d h:i:s', time());    
       $model->created = $currentTime; 

       $model->attributes=$_POST['User']; 

       if($_POST['User']['team']){ 
        $model->team_id = (int)$_POST['User']['team']; 
        $dep['team'] = (int)$_POST['User']['team']; 

       }elseif($_POST['User']['section']){ 
        $model->team_id = (int)$_POST['User']['section']; 
        $dep['section'] = (int)$_POST['User']['section']; 

       }elseif($_POST['User']['team_id']){ 
        $model->team_id = (int)$_POST['User']['team_id']; 
        $dep['department'] = (int)$_POST['User']['team_id']; 
       } 
       if($model->save()) 
        $this->redirect(array('view','id'=>$model->id)); 

      } 
     } 



     $this->render('create',array(
      'model'=>$model, 
     )); 
    } 

感謝你的幫助的規則。

======== ********** =========

更新的模式並沒有改變,

視圖中添加

'選項'=>數組( 「$模型 - >部」=>數組( '選擇'=> '選擇')),

成dorpdown

控制器

public function actionCreate() 
    { 
     $model=new User; 

     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['User'])) 
     { 
      if(User::model()->exists("login_name='".$_POST['User']['login_name']."'")) 
      { 
       $_SESSION['User'] = $_POST['User']; 

       $duplicate_error = '<p>Please fix the following input errors:</p> 
        <ul> 
        <li>Name is duplicated.</li> 
        </ul>'; 
       Yii::app()->user->setFlash('duplicate_error',$duplicate_error); 
       $this->redirect(array('user/create')); 
      }else{ 
       $currentTime = date('Y-m-d h:i:s', time());    
       $model->created = $currentTime; 

       $model->attributes=$_POST['User']; 
       $model->team = (int)$_POST['User']['team']; 
       $model->section = (int)$_POST['User']['section']; 


       if($_POST['User']['team']){ 
        $model->team_id = (int)$_POST['User']['team'];    
       }elseif($_POST['User']['section']){ 
        $model->team_id = (int)$_POST['User']['section'];    
       }elseif($_POST['User']['team_id']){ 
        $model->team_id = (int)$_POST['User']['team_id']; 
       } 


       if($model->save()) 
        $this->redirect(array('view','id'=>$model->id)); 

      } 
     } 

     //$dst = $this->getDST($model->team_id); 

     $this->render('create',array(
      'model'=>$model, 
      'department'=>0, 
      'section'=>0, 
      'team'=>0, 
     )); 
    } 

下拉空白問題已消失。但不驗證部分和團隊。我怎麼能夠?

+0

第一通知,'用戶::模型() - > exists'是錯誤的,在這一點上,請查看[CUniqueValidator](http://www.yiiframework.com/doc/api/1.1/CUniqueValidator)並在模型中使用。 – Pentium10 2012-07-26 09:20:33

+0

第二次通知,我沒有看到在這個場景的安全列表中看到任何rank_id,company_id,team_id,所以這可能是你的原因。 – Pentium10 2012-07-26 09:22:16

+0

你能解釋更多細節嗎?因爲我已經<1個月yii的經驗。 – 2012-07-26 09:36:53

回答

0

的驗證規則比較密碼丟失了compareAttribute參數:

array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'create'), 

HTH

+0

@nwd,當我將您的規則替換爲我的密碼比較規則時,驗證在更新表單中工作。如何解決? – 2012-07-30 02:52:14

+0

@tharsoe我不確定我是否理解問題或問題。 – nvwd 2012-07-30 11:47:39

+0

@nwd,密碼比較規則應該只在用戶創建表單上工作。但是當我將規則添加到我的模型規則函數中時,密碼字段驗證在刪除密碼和password_repeated字段後更新表單中工作。 – 2012-07-31 03:10:37