Yii下拉列表中的错误添加链接选项

时间:2012-04-02 15:21:13

标签: ajax yii

我是美丽的Yii Framework的新手。我正在浏览Yii的wiki文档。我得到了一个链接http://www.yiiframework.com/wiki/72/cjuidialog-and-ajaxsubmitbutton?,我得到了一些我需要的东西。所以我完成了教程之类的所有过程。但是我仍然无法从人员视图文件中保存job_name。

我的数据库就像这样

 ==== Job ====
  id
  job_name
  job_description
  created_at
  updated_at

  ==== Person ====
  id
  job_id
  person_name
  description
  created_at
  updated_at

我的_form.php for Person就像这样

 <?php echo $form->errorSummary($model); ?>
  <div class="row">
  <div id="job">
  <?php echo $form->dropDownList($model,'job_id',CHtml::listData(Job::model()->findAll(),'job_id','description'),array('prompt'=>'Select')); ?>
  <?php echo CHtml::ajaxLink(Yii::t('job','Create Job'),$this->createUrl('job/addnew'),array(
  'onclick'=>'$("#jobDialog").dialog("open"); return false;',
  'update'=>'#jobDialog'
  ),array('id'=>'showJobDialog'));?>
  <div id="jobDialog"></div>
  </div>
  </div>

作业控制器中的actionAddnew()就像这样

public function actionAddnew()
  {
    $model=new Job;
    // Ajax Validation enabled
    $this->performAjaxValidation($model);
    // Flag to know if we will render the form or try to add 
    // new jon.
    $flag=true;
    if(isset($_POST['Job']))
    {
      $flag=false;
      $model->attributes=$_POST['Job'];
      if($model->save()) 
      {
        //Return an <option> and select it
        echo CHtml::tag('option',array ('value'=>$model->jid,'selected'=>true),CHtml::encode($model->jdescr),true);
      }
     }
      if($flag) 
      {
        Yii::app()->clientScript->scriptMap['jquery.js'] = false;
        $this->renderPartial('createDialog',array('model'=>$model,),false,true);
      }
  }

根据教程,我在作业视图文件夹中创建了另外两个名为createDialog.php和_formDialog.php的文件。   createDialog.php的代码就像这样

<?php 
  $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
  'id'=>'jobDialog',
  'options'=>array(
  'title'=>Yii::t('job','Create Job'),
  'autoOpen'=>true,
  'modal'=>'true',
  'width'=>'auto',
  'height'=>'auto',
  ),
  ));
  echo $this->renderPartial('_formDialog', array('model'=>$model)); ?>
  <?php $this->endWidget('zii.widgets.jui.CJuiDialog');?>

_formDialog.php的代码就像这样

 <div class="form" id="jobDialogForm">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'job-form',
    'enableAjaxValidation'=>true,
)); 
//I have enableAjaxValidation set to true so i can validate on the fly the
?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'jid'); ?>
        <?php echo $form->textField($model,'jid',array('size'=>60,'maxlength'=>90)); ?>
        <?php echo $form->error($model,'jid'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'jdescr'); ?>
        <?php echo $form->textField($model,'jdescr',array('size'=>60,'maxlength'=>180)); ?>
        <?php echo $form->error($model,'jdescr'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::ajaxSubmitButton(Yii::t('job','Create Job'),CHtml::normalizeUrl(array('job/addnew','render'=>false)),array('success'=>'js: function(data) {
                        $("#Person_jid").append(data);
                        $("#jobDialog").dialog("close");
                    }'),array('id'=>'closeJobDialog')); ?>
    </div>

<?php $this->endWidget(); ?>

</div>

所以我错在哪里?任何帮助和建议都会非常明显。

1 个答案:

答案 0 :(得分:0)

将以下内容添加到Job模型中:

public function rules() {
  return array(
    ...
    array('job_name', 'safe'),
    ...
  );
}