1

状态:我在用户/创建时有 3 个依赖下拉自定义字段验证。他们验证得很好。Q : 提交后,如果没有通过验证,我所有的下拉列表都是空白的。我能怎么做?

这是在提交之前 提交前

这是提交后 提交后

这是部门 tbl 结构

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')),

进入 dorpdown

控制器

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,
        ));
    }

下拉空白问题消失了。但不验证部分和团队。我怎样才能?

4

1 回答 1

0

比较密码的验证规则缺少 compareAttribute 的参数:

array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'create'),

高温高压

于 2012-07-27T12:33:07.453 回答