我的应用程序的所有页面都需要“报告错误”模式表单。所以我以小部件的形式制作它并将它放在我的应用程序的主布局模板上。
<? $reportBugForm = $this->widget('application.widgets.reportBugWidget.reportBug',array(),true); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
....
</head>
<body>
<? echo $reportBugForm; ?>
我将它放在页面顶部,因为它应该向自身发布 ajax 请求,而不管当前页面如何(我不想使用任何控制器)。这是小部件类本身的代码:
Yii::import('application.widgets.reportBugWidget.reportBugForm');
class reportBug extends CWidget {
public $id = 'reportBug';
public $name = 'reportBugForm';
public $form;
public function init() {
$this->form = new reportBugForm('error');
if(Yii::app()->request->isAjaxRequest && isset($_POST[$this->name]) && !empty($_POST[$this->name])) {
$this->form->attributes = $_POST[$this->name];
if($this->form->validate()) {
echo 'Form is submited!';
} else
echo 'You have incorrectly filled out the form!';
Yii::app()->end();
}
}
public function run() {
$this->render('form',array(
'model'=>$this->form,
'id'=>$this->id,
'name'=>$this->name
));
}
}
这是我的模型的代码。我已经简化了所有可见性:
class reportBugForm extends CFormModel {
public $subject = '';
public $message = '';
public function rules() {
return array(
array('message','required','on'=>'error,question,suggestion'),
array('subject','safe','on'=>'question'),
array('subject','required','on'=>'error,suggestion')
);
}
public function attributeLabels() {
return array(
'subject'=>'Subject',
'message'=>'Message'
);
}
}
这是我的视图文件的代码:
<div class="modal fade hide" id="<? echo $id; ?>">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" id="reportBug_iconClose">×</button>
<h3>Report a bug</h3>
</div>
<?
$form = $this->beginWidget('CActiveForm', array(
'id'=>$name,
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'validateOnType'=>false
),
'focus'=>array($model,'subject')
));
?>
<div class="modal-body">
<fieldset>
<div class="control-group">
<?
echo $form->label($model,'subject',array('for'=>'reportBug_subject'));
echo $form->textField($model,'subject',array('id'=>'reportBug_subject','placeholder'=>'A short summary of your problem','class'=>'input-xxlarge'));
echo $form->error($model,'subject',array('class'=>'help-block error'));
?>
</div>
<div class="control-group">
<?
echo $form->label($model,'message',array('for'=>'reportBug_message'));
echo $form->textArea($model,'message',array('id'=>'reportBug_message','class'=>'input-xxlarge','rows'=>5,'placeholder'=>'Please, provide details of found error'));
echo $form->error($model,'message',array('class'=>'help-block error'));
?>
</div>
</fieldset>
</div>
<div class="modal-footer">
<?
echo CHtml::ajaxSubmitButton('Send',null,array(),array('class'=>'btn btn-primary'));
echo CHtml::htmlButton('Close',array('id'=>'reportBug_btnClose','class'=>'btn','data-dismiss'=>'modal'));
?>
</div>
<? $this->endWidget(); ?>
</div>
不执行客户端或 ajax 验证!表单由 ajax 提交,无需任何客户端验证,并You have incorrectly filled out the form!
返回字符串。请告诉我可能是什么?
PS我不知道这可能会有所不同,但我正在为 Yii 使用 Twitter Bootstrap 扩展。
PSS 还有一件奇怪的事情。当我CHtml::submitButton
在我的视图文件中使用时,表单只是通过 POST 提交,当我使用CHtml::ajaxSubmitButton
它时,通过 Ajax 提交并返回我上面描述的结果。