表单未在 yii 中验证和提交
Posted
技术标签:
【中文标题】表单未在 yii 中验证和提交【英文标题】:Form doesn't validate and submit in yii 【发布时间】:2015-09-27 11:08:19 【问题描述】:我尝试在 CJuiDialog 中通过 ajax 加载表单。表单已成功加载,但是当我提交表单或编写文本时,表单未验证且未提交。我尝试在 renderPartial 中设置“true”第四个参数,但之后对话框窗口没有打开。在控制台中,我收到错误
$(...).dialog 不是函数
鉴于我有这个:
Yii::app()->clientScript->registerScript(
"test",
"jQuery.ajax(
type: 'POST',
url: '".$this->createUrl("/Site/ShowForm")."',
success: function(html)
$('#form-test').html(html);
);
",
CClientScript::POS_READY); ?>
<div id="form-test"></div>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog');
我在控制器中的操作:
public function actionShowForm()
$model = new RegistrationForm;
if(Yii::app()->request->isAjaxRequest )
return $this->renderPartial('register', array('model' => $model),false,false);
public function actionRegister()
$model = new RegistrationForm;
$this->ajaxValidate($model);
$model->attributes =$_POST['RegistrationForm'];
if($model->validate())
$user = new User;
$user->attributes = $model->attributes;
if($user->save())
echo 1;
和我的表单(register.php)
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'register-form',
//'enableClientValidation'=>true,
'enableAjaxValidation' => true,
'clientOptions' => array(
//'validateOnSubmit' => true,
'validateOnChange' => true
),
'action' => array('site/Register'),
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model, 'first_name'); ?>
<?php echo $form->textField($model, 'first_name'); ?>
<?php echo $form->error($model, 'first_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'last_name'); ?>
<?php echo $form->textField($model, 'last_name'); ?>
<?php echo $form->error($model, 'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'email'); ?>
<?php echo $form->emailField($model, 'email'); ?>
<?php echo $form->error($model, 'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'password'); ?>
<?php echo $form->textField($model, 'password'); ?>
<?php echo $form->error($model, 'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'Repeat password'); ?>
<?php echo $form->textField($model, 'repeat_password'); ?>
<?php echo $form->error($model, 'repeat_password'); ?>
</div>
<div>
<?php echo $form->textField($model, 'verifyCode'); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->error($model, 'verifyCode'); ?>
</div>
<div class="row buttons">
<?php
echo CHtml::ajaxSubmitButton('Register', $this->createUrl("/Site/Register"), array(
'type' => 'POST',
'dataType' => 'json',
'success' => 'js:function(data)
if(data == 1)
window.location ="' . $this->createUrl('site/index') . '"
',
));
?>
</div>
<?php $this->endWidget(); ?>
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:在您检查请求是否为 Ajax 的控制器中,您应该在那里进行 Ajax 验证。
【讨论】:
这不起作用,当我把 this->ajaxValidate($model);在 renderPartial 方法之后的 showaction 中,我得到了这样的简单文本“”RegistrationForm_first_name”:[“First Name cannot be blank.”],“RegistrationForm_ ...”。此外,SwhoAction 必须仅显示表单【参考方案2】:我找到了解决方案!我加了
Yii::app()->clientScript->scriptMap = array(
'jquery.js' => false,
'jquery.ui.js' => false,
'jquery.yiiactiveform.js' => false,
);
在我的 actionShowForm 中。
【讨论】:
以上是关于表单未在 yii 中验证和提交的主要内容,如果未能解决你的问题,请参考以下文章
html/javascript onsubmit 事件未在表单提交上调用以验证表单
Yii框架用ajax提交表单时候报错Bad Request (#400): Unable to verify your data submission.