我有这样的表格:
<?php echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data')); ?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'userName'); ?>
<?php echo $form->textField($model,'userName',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
<?php echo $form->error($model,'userName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>45,'maxlength'=>30, 'id'=>'in')); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'firstName', array('id'=>'in0')); ?>
<?php echo $form->textField($model,'firstName',array('size'=>45,'maxlength'=>50, 'id'=>'in1')); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName', array('id'=>'in2')); ?>
<?php echo $form->textField($model,'lastName',array('size'=>45,'maxlength'=>50, 'id'=>'in3')); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'birthDate', array('id'=>'birth0')); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'name'=>'publishDate',
'attribute'=>'birthDate',
'model'=>$model,
'language'=>'en',
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'altFormat'=>'dd-mm-yy',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array('id'=>'birth'),)); ?>
<?php echo $form->error($model,'birthDate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender', array('id'=>'in4')); ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'0', 'id'=>'in5')) . '<b id=in6 style="height:20px; color: #000666; font-size: 12pt;
font-weight: bold;"> Male</b><br>'; ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'1', 'id'=>'in7')) . '<b id=in8 style="height:20px; color: #000666; font-size: 12pt;
font-weight: bold;"> Female</b>'; ?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'idCountry'); ?>
<?php $country=Countries::model()->findAll();
$countryList=CHtml::listData($country, 'idCountry' , 'cntName');
$url = CController::createUrl('users/selectedcity');
echo CHtml::activeDropDownList($model,'idCountry', $countryList,
array('empty'=>'choose country','id' => 'idCountry','onchange' =>
"selectedcountry('{$url}', 'idCountry', '#name');",'style'=>'width:300px')); ?>
<?php echo $form->error($model,'idCountry'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'idCity'); ?>
<div id="name"></div>
<?php echo $form->error($model,'idCity'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'securityQuestion'); ?>
<?php $q= Securityquestions::model()->findAll();
$qs=CHtml::listData($q, 'idQuestion' , 'question');
echo CHtml::activeDropDownList($model,'securityQuestion', $qs,array('empty'=>'Select question', 'id'=>'sec')); ?>
<?php echo $form->error($model,'securityQuestion'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'answer'); ?>
<?php echo $form->textField($model,'answer',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
<?php echo $form->error($model,'answer'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'facebookAcc'); ?>
<?php echo $form->textField($model,'facebookAcc',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
<?php echo $form->error($model,'facebookAcc'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'twitterAcc'); ?>
<?php echo $form->textField($model,'twitterAcc',array('size'=>45,'maxlength'=>255, 'id'=>'in')); ?>
<?php echo $form->error($model,'twitterAcc'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'profilePhoto'); ?>
<?php echo CHtml::radioButton('profile',false,array('value'=>'0')) .
'<b style="height:20px; color: #000666;
font-size: 12pt; font-weight: bold;">
Defalut</b><br>'; ?>
<?php echo CHtml::radioButton('profile',false,array('value'=>'1')) .
'<b style="height:20px; color: #000666;
font-size: 12pt; font-weight: bold;">
Upload from here :</b>'; ?>
<?php echo CHtml::activeFileField($model,'profilePhoto',array('width'=>45)); ?>
<?php echo $form->error($model,'profilePhoto'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'website'); ?>
<?php echo $form->textField($model,'website',array('size'=>45,'maxlength'=>500, 'id'=>'in')); ?>
<?php echo $form->error($model,'website'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'telephone'); ?>
<?php echo $form->textField($model,'telephone',array('size'=>45,'maxlength'=>200, 'id'=>'in')); ?>
<?php echo $form->error($model,'telephone'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('id'=>'but')); ?>
<?php echo CHtml::resetButton('Reset', array('id'=>'but')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php echo CHtml::endForm(); ?>
这是表单所属的操作:
$model=new Users;
$countries=Countries::model()->findAll();
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$model->userType='p';
$model->communityType='null';
$model->active=1;
$model->admin=0;
if($model->gender==0)
$model->gender='m';
else
$model->gender='f';
if(isset($_POST['profile'])){
if($_POST['profile']==0)
$model->profilePhoto='default.jpg';
elseif($_POST['profile']==1)
$model->profilePhoto=CUploadedFile::getInstance($model, 'profilePhoto');
}
if($model->save()){
$this->actionfiledir();
if(($_POST['profile'])==1)
$model->profilePhoto->saveAs($this->actionphotodir()."/".$model->profilePhoto);
Yii::app()->session['userId']=$model->idUser;
$this->redirect(array('view','id'=>$model->idUser));
}
}
$this->render('createp',array(
'model'=>$model,'countries'=>$countries
));
}
else
$this->redirect(array('site/index'));
}
public function actionCreatec()
{
if($this->actionlogged()==0||$this->actionisadmin()==1)
{
$model=new Users;
$model2=new Communities;
$countries=Countries::model()->findAll();
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation($model);
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
$model->userType='c';
$model->gender='n';
$model->firstName='null';
$model->lastName='null';
$model->birthDate='null';
$model->active=1;
$model->admin=0;
if(isset($_POST['profile'])==0)
$model->profilePhoto='default.jpg';
elseif(isset($_POST['profile'])==1)
$model->profilePhoto=CUploadedFile::getInstance($model, 'profilePhoto');
if($model->save()){
$model2->idUser=$model->idUser;
$model2->admin=1;
$model2->save(false);
$this->actionfiledir();
if(isset($_POST['profile'])==1)
$model->profilePhoto->saveAs($this->actionphotodir()."/".$model->profilePhoto);
Yii::app()->session['userId']=$model->idUser;
$this->redirect(array('view','id'=>$model->idUser));
}
}
$this->render('createc',array(
'model'=>$model,'countries'=>$countries
));
提交表单后,如果表单有验证错误,它将返回给用户重新填写并更正错误,但表单没有返回其全部值,因此选中的单选按钮将返回未选中,还有城市的dropDownList,那么如果没有提交,如何保留表单的所有值? 我希望我的问题很明确。
答案 0 :(得分:0)
启用提交验证,如果表单有错误,则不会提交表单。
'clientOptions'=>array(
'validateOnSubmit'=>true, // Required to perform AJAX validation on form submit
),
在
中添加此值 $form=$this->beginWidget('CActiveForm', array(
'id'=>'users-form',
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));