我正在尝试使用AJAX调用和renderPartial()在模式对话框中打开包含yiibooster TbSelect2的表单。它抛出了这个错误:
未捕获的异常:未为Select2 Patient_state
定义查询功能只有当我尝试使用AJAX调用填充模态时才会发生这种情况。如果我只是加载带有表单的页面,则工作正常。请帮忙!
这是代码: 控制器:
public function actionCreate()
{
$model = new Patient;
$this->_createUpdate($model);
}
private function _createUpdate($model)
{
if (isset($_POST['Patient'])) {
$model->attributes = $_POST['Patient'];
if ($model->save()) {
if (Yii::app()->request->isAjaxRequest) {
echo CJSON::encode($model->attributes);
Yii::app()->end();
} else {
$this->redirect(array('admin'));
}
}
}
if (Yii::app()->request->isAjaxRequest) {
$this->renderPartial('_form', array('model' => $model), false, true);
} else {
$this->render('update', array('model' => $model));
}
}
形式:
<?php
/* @var $this PatientController */
/* @var $model Patient */
/* @var $form CActiveForm */
if (Yii::app()->request->isAjaxRequest) {
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
}
?>
<div class="form" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<?php $form=$this->beginWidget('CActiveForm', array(
'id' => 'patient-form',
'enableAjaxValidation' => false,
)); ?>
<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', array('size' => 37,'maxlength' => 100)); ?>
<?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', array('size' => 37,'maxlength' => 100)); ?>
<?php echo $form->error($model,'last_name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'addr1'); ?>
<?php echo $form->textField($model,'addr1', array('size' => 37,'maxlength' => 256)); ?>
<?php echo $form->error($model,'addr1'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'city'); ?>
<?php echo $form->textField($model,'city', array('size' => 37,'maxlength' => 100)); ?>
<?php echo $form->error($model,'city'); ?>
</div>
<div class="row">
<div class="inline">
<?php echo $form->labelEx($model,'state'); ?>
<?php
$this->widget(
'booster.widgets.TbSelect2',
array(
'model' => $model,
'attribute' => 'state',
'data' => array('AL' => 'Alaska', 'CA' => 'California'),
'value' => $model->state,
'options' => array(
'placeholder' => 'Select',
)
)
);
?>
<?php echo $form->error($model,'state'); ?>
</div>
<div class="inline" style="margin-left: 10px;">
<?php echo $form->labelEx($model,'postal_code'); ?>
<?php echo $form->textField($model,'postal_code', array('size' => 10,'maxlength' => 10)); ?>
<?php echo $form->error($model,'postal_code'); ?>
</div>
</div>
<div class="row">
<?php echo $form->labelEx($model,'dob'); ?>
<?php echo $form->textField($model,'dob', array('id' => 'dob-input')); ?>
<div id="dob-hints" class="hint">MM/DD/YYYY
</div>
<?php echo $form->error($model,'dob'); ?>
</div>
<div class="row buttons">
<?php
if ($model->isNewRecord) {
$buttonLabel = 'Create';
$url = $this->createUrl('create');
} else {
$buttonLabel = 'Save';
$url = $this->createUrl('update', array("id" => $model->id));
}
?>
<?php $this->widget(
'booster.widgets.TbButton',
array(
'label' => $buttonLabel,
'context' => 'primary',
'buttonType' => 'link',
'htmlOptions' => array(
'id' => 'submit-button',
'onClick' => 'submit();')
)
);
?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->