2014-06-26 24 views
0

我是Yii框架的新手。目前,我有一個需要我使用Yii框架的項目。我想問一下,是否可以驗證數據庫中沒有保存的屬性?Yii Framework:在查看頁面驗證複選框

case: 我有一個複選框,需要用戶在它上面打勾才能移動到下一頁。如果用戶不勾選它,則會提示錯誤。如何以Yii格式驗證它?

有人可以教我如何改變下面的驗證,以適應Yii格式?驗證位置在哪裏?

在model.php內容

public $pdpa_agree; 

public function rules() 
{ 
    array('pdpa_agree', 'required'); 
} 

內容view.php

<?php 
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
    'id'=>'pdpaPolicy-form', 
    'enableAjaxValidation'=>true, 
    'type'=>'horizontal', 
    'htmlOptions' => array(
     'enctype' => 'multipart/form-data', 
     "autocomplete"=>"off", //turn off auto complete in FF 
    ) 
)); 
?> 

<?php echo $data->pdpa_content; ?> 

<p class="cb_pdpa" style="font-weight:bold"><?php echo $form->checkbox($data,'pdpa_agree'); ?>&nbsp;&nbsp;&nbsp;I have read and understood the above policies and hereby give consent for CTES to use my <pd>*personal data</pd> in accordance to the policies listed out above.</p> 

<div class="form-actions"> 
    <?php 
    /*$this->widget('bootstrap.widgets.TbButton', array(
     'buttonType' => 'submit', 
     'type' => 'primary', 
     'label'=>$model->isNewRecord ? 'PolicyAgreement' : 'Continue Registration', 
    ));*/ 
    ?> 
    <input type="button" name="submit" value="Continue Registration" onclick="validateAgreement()"> 
</div> 

<?php $this->endWidget(); ?> 

<script> 
function validateAgreement() 
{ 
    if($("#pdpa_agree").is(':checked')) 
    { 
     window.location.href = 'register?sourceID=CTES'; 
     return true; 
    } 
    else 
    { 
     alert("Please tick on the agreement checkbox in order to proceed the registration!"); 
     return false; 
    } 
} 
</script> 

如何打開下面的驗證,以適應Yii的格式?

<script> 
function validateAgreement() 
{ 
    if($("#pdpa_agree").is(':checked')) 
    { 
     window.location.href = 'register?sourceID=CTES'; 
     return true; 
    } 
    else 
    { 
     alert("Please tick on the agreement checkbox in order to proceed the registration!"); 
     return false; 
    } 
} 
</script> 

回答

0

是的,你就可以驗證

Model.php 

Delclare希望變量使用

public $pdpa_agree; 

public function rules() 
{ 
    array('pdpa_agree', 'required'); 
} 

公共函數attributeLabels() { 返回陣列( 'pdpa_agree'=>'我已閱讀並理解上述政策,特此同意CTES根據p。使用我的*個人資料上面列出的政策', ); }

MyController.php

public function actionRegistration(){ 
    $model = new Model(); 

    if(isset($_POST['Model'])){ 
     //Stuff to save Goes here 
    } 
    $this->render('registration'); 
} 

view.php

<?php 
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
    'id'=>'pdpaPolicy-form', 
    'enableAjaxValidation'=>true, 
    'enableClientValidation'=>true, 
    'type'=>'horizontal', 
    'htmlOptions' => array(
     'enctype' => 'multipart/form-data', 
     "autocomplete"=>"off", //turn off auto complete in FF 
    ) 
)); 
?> 

<?php echo $data->pdpa_content; ?> 

<div class="form-actions">   
    $form->checkBox($model,'checkBox'); 
    $form->labelEx($model,'checkBox'); 
    $form->error($model,'checkBox'); 
</div> 

<?php $this->endWidget(); ?> 
+0

謝謝。那麼我想知道,我該如何做驗證? – Verlee

+0

你需要什麼驗證? –

+0

嗨,我已編輯上述問題。我想知道如何更改上面的驗證以適應Yii格式?我應該在哪裏進行驗證? – Verlee