使用Yii-Bootstrap扩展创建模态登录窗口小部件时出现错误。
CException错误 - TbInput:无法初始化窗口小部件!模型未设置。
有什么不对?
这是我的源代码 -
登录按钮代码 -
in /views/layouts/main.php
<?php if (Yii::app()->user->isGuest): ?>
$this->widget('bootstrap.widgets.TbButton', array(
'size' => 'large',
'label' => 'login',
'url' => '#login-modal',
'htmlOptions' => array(
'data-toggle' => 'modal',
'onclick' => '$("#error-div").hide();$("#LoginForm_username").focus();'),
)
);
endif;
$this->renderPartial("_ajax_login_form");
?>
的 ajax_login_form.php
<?php $this->beginWidget('bootstrap.widgets.TbModal', array(
'id' => 'login-modal'
)
); ?>
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>login</h3>
</div>
<div class="modal-body">
<?php
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'LoginForm',
'htmlOptions' => array(
'class' => 'well'),
)
);
?>
<div id="error-div" class="alert alert-block alert-error" style="display:none;">
</div>
<?php echo $form->textFieldRow($login_form_model, 'username', array(
'class' => 'span3'
)
); ?>
<?php echo $form->passwordFieldRow($login_form_model, 'password', array(
'class' => 'span3'
)
); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType' => 'ajaxSubmit',
'icon' => 'ok',
'label' => 'Submit',
'ajaxOptions' => array(
'success' =>
'function(data){
var obj = $.parseJSON(data);
if(obj.login=="success"){
$("#login-modal").modal("hide");
setTimeout(function(){location.reload(true);},400);
}else{
$("#error-div").show();
$("#error-div").html("");
if("LoginForm_password" in obj){
$("#error-div").html(obj.LoginForm_password[0]+"<br />");
}
if("LoginForm_username" in obj){
$("#error-div").append(obj.LoginForm_username[0]);
}
}
}'),
));
$this->endWidget(); ?>
</div>
<?php $this->endWidget(); ?>
的 SiteController.php
public function actionLogin(){
//$this->layout = 'main';
$model = new LoginForm();
if(isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
{
echo CActiveForm::validate($model, array('username', 'password', 'verifyCode'));
Yii::app()->end();
}
if (isset($_POST['LoginForm'])) {
$login_form_model->attributes = $_POST['LoginForm'];
if ($login_form_model->validate() and $login_form_model->login()) {
$array = array('login' => 'success');
Yii::app()->user->setFlash('success', 'Successfully logged in.');
$json = json_encode($array);
echo $json;
Yii::app()->end();
} else {
echo CActiveForm::validate($login_form_model);
Yii::app()->end();
}
}
$this->render('login', array(
'model' => $model,
));
}
的login.php
<?php
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'login-form',
'enableClientValidation' => true,
'htmlOptions' => array('class' => 'well'),
'clientOptions' => array(
'validateOnSubmit' => true,
),
)); ?>
<?php echo $form->textFieldRow($model, 'username', array('class' => 'span3'));?>
<?php echo $form->passwordFieldRow($model, 'password', array('class' => 'span3'));?>
<?php echo $form->checkBoxRow($model, 'rememberMe');?>
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => 'Submit', 'icon' => 'ok'));?>
<?php $this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'reset', 'label' => 'Reset'));?>
<?php $this->endWidget(); ?>
请帮我解决我的错误。
答案 0 :(得分:1)
生成错误是因为您未将$model
正确传递给视图。
将actionLogin()
中的SiteController.php
更改为
public function actionLogin(){
//$this->layout = 'main';
$model = new LoginForm();
if(isset($_POST['ajax']) && $_POST['ajax'] === 'login-form')
{
echo CActiveForm::validate($model, array('email', 'password', 'verifyCode'));
Yii::app()->end();
}
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
if ($model->validate() and $model->login()) {
$array = array('login' => 'success');
Yii::app()->user->setFlash('success', 'Successfully logged in.');
$json = json_encode($array);
echo $json;
Yii::app()->end();
} else {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
$this->render('login', array(
'model' => $model,
));
}
main.php文件中需要的更改
$login_form_model = new LoginForm();
$this->renderPartial("_ajax_login_form", array(
'login_form_model' => $login_form_model,
)
);