我想用Ajax验证方法验证我的表单。所以我已经更改了这样的视图文件的代码。
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'customers-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstname,lastname,gender,email,contact_no,address,state,country'),
)); ?>
<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,'firstname'); ?>
<?php echo $form->textField($model,'firstname',array('size'=>60,'maxlength'=>80)); ?>
<?php echo $form->error($model,'firstname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastname'); ?>
<?php echo $form->textField($model,'lastname',array('size'=>60,'maxlength'=>80)); ?>
<?php echo $form->error($model,'lastname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->radioButtonList($model,'gender',array('Male'=>'Male','Female'=>'Female'),array(
'labelOptions'=>array('style'=>'display:inline;padding-right:10px'),'separator'=>'',));?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'contact_no'); ?>
<?php echo $form->textField($model,'contact_no',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'contact_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address'); ?>
<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>120)); ?>
<?php echo $form->error($model,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->textField($model,'state',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'state'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country'); ?>
<?php echo $form->textField($model,'country',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'country'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
我已经为这个模型做了这样的规则
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('firstname,lastname,gender,email,contact_no,address,state,country', 'required'),
array('firstname, lastname', 'length', 'max'=>80),
);
}
在完成所有这些代码后,我发现所有字段都以正常方式显示验证,但在进行Ajax验证时,唯一的Firstname, Lastname, Email
显示ajax验证。字段的恢复未显示任何ajax验证有人告诉我,我在哪里做错了吗?任何帮助和建议都会非常值得注意。
的被修改
以下是处理Ajax验证的控制器代码
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='customers-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
答案 0 :(得分:0)
只需在模特的actionCreate()
中使用它 public function actionCreate()
{
$model=new Customers;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Customers']))
{
if(Yii::app()->getRequest()->getIsAjaxRequest())
{
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
$model->attributes=$_POST['Customers'];
$valid = $model->validate();
if($valid)
{
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
用于查看的其余文件将与上述相同。
答案 1 :(得分:0)
我试过了,你可以这样说:
public function actionCreate()
{
$model=new Main;
if(isset($_POST['ajax']) )
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['Main']))
在视图中:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'main-form',
'enableAjaxValidation'=>true,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),))
'确实需要'