所以这就是我正在做的事情,我在表单中创建了一个依赖的下拉菜单,我在我的控制器上验证它:
public function actionCreate()
{
$model=new Management;
if(isset($_POST['Management']))
{
if($_POST['Management']['company_id']==''){
//error please select a company
Yii::app()->user->setFlash('company_error',"Please select a company.");
$this->redirect(array('create'));
}else{
if($_POST['Management']['site_id']==''){
//error please select a site
Yii::app()->user->setFlash('site_error',"Please select a site.");
Yii::app()->user->setFlash('cid',$_POST['Management']['company_id']);
$this->redirect(array('create'));
}else{
//save
$model->attributes=$_POST['Management'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,'update'=>''
));
}
我正在将公司ID传递给我的表单视图,如果验证失败,我想保留上一个下拉列表。这就是我的下拉列表:
<div class="row">
<?php echo $form->labelEx($model,'company_id'); ?>
<?php echo $form->dropDownList($model, 'company_id', $company, /*array('options'=>array($cid=>array('selected'=>true))),*/
array(
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('Management/dropDownSite'),
'update'=>'#SectorManagement_site_id',
'data'=>array('CompanyId'=>'js:this.value'),
)));
?>
<?php echo $form->error($model,'company_id'); ?>
</div>
现在当我取消注释代码时:
array('options'=>array($cid=>array('selected'=>true))),
从上面的下拉列表中显示预先选择的值,但随后依赖下拉列表不起作用。任何想法我应该如何解决这个问题。提前致谢。 :)
答案 0 :(得分:0)
我认为你正在以错误的方式看待这个问题。如果您的验证失败,您可以重定向,而您已经在页面上进行重定向。
我不是100%肯定,但我认为你在行动中寻找的是
public function actionCreate()
{
$model = new Management;
if (isset($_POST['Management'])) {
$model->attributes = $_POST['Management'];
if ($_POST['Management']['company_id'] == '') {
//error please select a company
Yii::app()->user->setFlash('company_error', "Please select a company.");
} elseif ($_POST['Management']['site_id'] == '') {
//error please select a site
Yii::app()->user->setFlash('site_error', "Please select a site.");
Yii::app()->user->setFlash('cid', $_POST['Management']['company_id']);
} elseif ($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render(
'create',
array(
'model' => $model,
'update' => ''
)
);
}
您的观点可能会保持原样。
为了进一步改善这一点,您应该在模型中处理验证,如:
public function rules()
{
return array(
array('company_id, site_id', 'required'),
);
}
然后你需要做的就是:
public function actionCreate()
{
$model = new Management;
if (isset($_POST['Management'])) {
$model->attributes = $_POST['Management'];
if ($model->validate() && $model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render(
'create',
array(
'model' => $model,
'update' => ''
)
);
}
答案 1 :(得分:0)
我终于想通了,这就是它对我有用的方式:
public function actionCreate()
{
$model=new Management;
if(isset($_POST['Management']))
{
if($_POST['Management']['company_id']==''){
//error please select a company
Yii::app()->user->setFlash('company_error',"Please select a company.");
$this->redirect(array('create'));
}else{
if($_POST['Management']['site_id']==''){
//error please select a site
Yii::app()->user->setFlash('site_error',"Please select a site.");
$model->company_id = $_POST['Management']['company_id'];
$this->render('create',array(
'model'=>$model,
'update'=>'',
));
exit;
}else{
//save
$model->attributes=$_POST['Management'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,'update'=>''
));
}