我是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'); ?> 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 :(得分:0)
是的,你可以验证
Model.php
Delclare您要使用的变量
public $pdpa_agree;
public function rules()
{
array('pdpa_agree', 'required');
}
public function attributeLabels() { 返回数组( 'pdpa_agree'=&gt; '我已阅读并理解上述政策,并在此同意CTES根据上述政策使用我的*个人数据', ); }
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(); ?>