status : user/create で 3 つの依存ドロップダウン カスタム フィールド検証を行いました。彼らはうまく検証しています。Q : 送信後、検証に合格しなかった場合、ドロップダウン リストがすべて空白になります。どのようにできるのか?
提出前です
提出後です
これは部門テーブル構造です
department
id | name | p_id | company_id
1 | dep1 | 0 | 1
2 | dep2 | 0 | 1
3 | sec1.1 | 1 | 1
4 | sec2.1 | 2 | 1
5 | team1.1.1 | 3 | 1
6 | team1.1.2 | 3 | 1
7 | team2.1.1 | 4 | 1
これはユーザーの tbl 構造です
user
id | name | company_id | team_id
team_id と部門 tbl の関係。
これはユーザーモデルのルールです
public $section;
public $team;
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('login_name, email, rank_id, company_id, team_id, section, team', 'required'), // team_id is Department
array('rank_id, company_id, team_id', 'numerical', 'integerOnly'=>true),
array('login_name, first_name, last_name', 'length', 'max'=>100),
array('password', 'length', 'max'=>50),
array('email', 'length', 'max'=>200),
array('password', 'compare', 'on' => 'create'),
array('password_repeat', 'safe', 'on' => 'create'),
array('password, password_repeat', 'required', 'on' => 'create'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('first_name, last_name, email, created, updated, rank_id, company_id, team_id', 'safe', 'on'=>'search'),
);
}
このビュー (_form.php)
<!-- This is Department -->
<div class="row">
<?php
if($model->company_id)
{
$records = Department::model()->findAll('p_id=0 AND company_id=:company_id', array(':company_id'=>(int) $model->company_id));
$department_list = CHtml::listData($records, 'id', 'name');
}else $department_list = array();
?>
<?php echo $form->labelEx($model,'team_id'); ?>
<?php echo $form->dropDownList($model,'team_id', $department_list, array('prompt'=>'Please select a department',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('user/dynamicsection'), //url to call.
'dataType'=>'json',
'data'=>array('department_id'=>'js:this.value'),
'success'=>'function(data) {
$("#User_section").html(data.ddsection);
$("#User_team").html(data.ddteam);
}',
)
)
);
?>
<?php echo $form->error($model,'team_id'); ?>
</div>
<!-- This is section -->
<div class="row">
<?php echo $form->labelEx($model,'section'); ?>
<?php echo $form->dropDownList($model,'section', array(), array('prompt'=>'Please select a section',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('user/dynamicteam'), //url to call.
'dataType'=>'json',
'data'=>array('section'=>'js:this.value'),
'success'=>'function(data) {
$("#User_team").html(data.ddteam);
}',
))); ?>
<?php echo $form->error($model,'section'); ?>
</div>
<!-- This is Team -->
<div class="row">
<?php echo $form->labelEx($model,'team'); ?>
<?php echo $form->dropDownList($model,'team', array(), array('prompt'=>'Please select a team')); ?>
<?php echo $form->error($model,'team'); ?>
</div>
このコントローラー
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
if(User::model()->exists("login_name='".$_POST['User']['login_name']."'"))
{
$_SESSION['User'] = $_POST['User'];
$duplicate_error = '<p>Please fix the following input errors:</p>
<ul>
<li>Name is duplicated.</li>
</ul>';
Yii::app()->user->setFlash('duplicate_error',$duplicate_error);
$this->redirect(array('user/create'));
}else{
$currentTime = date('Y-m-d h:i:s', time());
$model->created = $currentTime;
$model->attributes=$_POST['User'];
if($_POST['User']['team']){
$model->team_id = (int)$_POST['User']['team'];
$dep['team'] = (int)$_POST['User']['team'];
}elseif($_POST['User']['section']){
$model->team_id = (int)$_POST['User']['section'];
$dep['section'] = (int)$_POST['User']['section'];
}elseif($_POST['User']['team_id']){
$model->team_id = (int)$_POST['User']['team_id'];
$dep['department'] = (int)$_POST['User']['team_id'];
}
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
お手伝いありがとうございます。
========************=========
更新されたモデルは変更されませんでしたが、
追加する
'options'=>array("$model->section"=>array('selected'=>'selected')),
ドロップダウンに
コントローラ
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
if(User::model()->exists("login_name='".$_POST['User']['login_name']."'"))
{
$_SESSION['User'] = $_POST['User'];
$duplicate_error = '<p>Please fix the following input errors:</p>
<ul>
<li>Name is duplicated.</li>
</ul>';
Yii::app()->user->setFlash('duplicate_error',$duplicate_error);
$this->redirect(array('user/create'));
}else{
$currentTime = date('Y-m-d h:i:s', time());
$model->created = $currentTime;
$model->attributes=$_POST['User'];
$model->team = (int)$_POST['User']['team'];
$model->section = (int)$_POST['User']['section'];
if($_POST['User']['team']){
$model->team_id = (int)$_POST['User']['team'];
}elseif($_POST['User']['section']){
$model->team_id = (int)$_POST['User']['section'];
}elseif($_POST['User']['team_id']){
$model->team_id = (int)$_POST['User']['team_id'];
}
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
}
//$dst = $this->getDST($model->team_id);
$this->render('create',array(
'model'=>$model,
'department'=>0,
'section'=>0,
'team'=>0,
));
}
ドロップダウンのブランクアウトの問題はなくなりました。ただし、セクションとチームは検証しません。どうやって?