0
votes

I'm new to the Yii Framework. Currently, I'm having a project which require me to use Yii framework. I would like to ask, is it possible for me to validate an attribute which is not save inside the database?

case: I have a checkbox which require the user to tick on it in order to move to the next page. If the user doesn't tick on it, then it will prompt an error. How to I validate it in Yii format?

Can someone teach me how to change the validation below to fit Yii Format? where should the validation locate?

content in model.php

public $pdpa_agree;

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

content in 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>

How to turn to validation below to fit Yii Format?

<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>
1

1 Answers

0
votes

Yeah you can validate

Model.php

Delclare the variable you want to use

public $pdpa_agree;

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

public function attributeLabels() { return array( 'pdpa_agree' => 'I have read and understood the above policies and hereby give consent for CTES to use my *personal data in accordance to the policies listed out above', ); }

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