View Yii框架:查看页面上的验证复选框

View Yii框架:查看页面上的验证复选框,view,yii,View,Yii,我是Yii框架的新手。目前,我有一个项目需要我使用Yii框架。我想问,我是否可以验证未保存在数据库中的属性 案例: 我有一个复选框,它要求用户勾选它,以便移动到下一页。如果用户没有勾选,则会提示错误。如何以Yii格式验证它 有人能教我如何更改下面的验证以适应Yii格式吗?验证应位于何处 model.php中的内容 public $pdpa_agree; public function rules() { array('pdpa_agree', 'required'); } view.

我是Yii框架的新手。目前,我有一个项目需要我使用Yii框架。我想问,我是否可以验证未保存在数据库中的属性

案例: 我有一个复选框,它要求用户勾选它,以便移动到下一页。如果用户没有勾选,则会提示错误。如何以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>

本人已阅读并理解上述政策,特此同意CTE根据上述政策使用本人的*个人数据

函数validateAgreement() { 如果($(“#pdpa_agree”)。为(“:选中”) { window.location.href='register?sourceID=CTES'; 返回true; } 其他的 { 警告(“请勾选协议复选框以继续注册!”); 返回false; } }
如何在下面进行验证以适应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>

函数validateAgreement()
{
如果($(“#pdpa_agree”)。为(“:选中”)
{
window.location.href='register?sourceID=CTES';
返回true;
}
其他的
{
警告(“请勾选协议复选框以继续注册!”);
返回false;
}   
}
是的,您可以验证

Model.php
Delclare要使用的变量

public $pdpa_agree;

public function rules()
{
    array('pdpa_agree', 'required');
}
公共函数attributeLabels() { 返回数组( “pdpa_agree”=>“我已阅读并理解上述政策,特此同意CTE根据上述政策使用我的*个人数据”, ); }

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(); ?>

$form->checkBox($model,'checkBox');
$form->labelEx($model,'checkBox');
$form->error($model,'checkBox');

谢谢。那么我想知道,我如何进行验证?你需要什么样的验证?嗨,我已经编辑了上面的问题。我想知道如何更改上述验证以适应Yii格式?我应该将验证放在哪里?顺便说一句,验证函数在哪里?enableClientValidation=>true客户端验证是否基于模型