我的应用程序的所有页面上都需要“Report Bug”模式表单。所以我以小部件的形式制作它并将它放在我的应用程序的主要布局模板上。
<? $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请求,而不管当前页面(我不想使用任何控制器)。这是widget类本身的代码:
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'
);
}
}
以下是我的View文件的代码:
<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!
字符串。请告诉我它会是什么?
P.S。我不知道也许它有所不同,但我正在使用Twitter Bootstrap扩展Yii。
P.S.S。还有一件奇怪的事情。当我在我的视图文件中使用CHtml::submitButton
时,表单只是通过POST提交,当我使用CHtml::ajaxSubmitButton
时,它会通过Ajax提交并返回我上面描述的结果。
答案 0 :(得分:1)
此指令仅适用于&#34; safe&#34; PARAMS:
$this->form->attributes = $_POST[$this->name];
您必须添加&#34;消息&#34;安全验证器:
array('subject,message','safe','on'=>'question'),
或者您必须使用:
$this->form->subject = $_POST[$this->name]['subject'];
$this->form->message = $_POST[$this->name]['message'];
请参阅此文档: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules
您的验证无效,因为&#34; message&#34;是必需的,但不是&#34;安全&#34;。