验证 CakePHP 中具有相同名称字段的多个下拉列表 2.x

validating mutiple drop downs list with the same name field in CakePHP 2.x

我是 cakephp 2x 的新手。我的表单中有多个下拉列表。所有下拉列表都使用相同的列表数据,来自一个名为 -> Students 的 table。现在我想要的是,当用户 select 第一个学生在第一个下拉列表中时,该学生应该在接下来的四个下拉列表中减去或在接下来的四个下拉列表中禁用 downs.So 用户不会能够重复添加同一个学生。请帮助!谢谢!提前。

      //Here is my controller add file:
        public function add(){
        if ($this->request->is('post')) {
     $this->Group->create()
   if ($this->Group->save($this->request->data)) {
            $this->Session->setFlash(__('The group has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The group could not be saved.  Please, try again.'));
        }

    }


    $this->loadModel('Student');
    $student1s = $this->Student->find('list');

    //pr ($student1s);
     $this->set(compact('student1s'));
  }

// Here is my add.ctp

 < div class="Groups form">

 <div class="row">
    <div class="col-md-12">
        <div class="page-header">
            <h1><?php echo __('Add Students'); ?></h1>
        </div>
    </div>
</div>



<div class="row">
    <?php echo $this->Form->create('Groups', array('role' => 'form')); ?>
    <div class="col-md-5 col-md-offset-3">


            <div class="form-group">
                <?php echo $this->Form->input('0.name',  array('type'=>'select','options'=>$student1s,'empty' => 'Please choose a  student','class'=>'form-control'));?>
            </div>
            <div class="form-group" >
                <?php echo $this->Form->input('1.name',  array('type'=>'select',"options"=>$student1s,'empty' => 'Please choose a  student','class'=>'form-control',));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('2.name', array('type'=>'select',"options"=>$student1s,'empty' => 'Please choose a   student','class'=>'form-control'));?>
            </div>


            <div class="form-group">
                <?php echo $this->Form->input('3.name', array('type'=>'select',"options"=>$student1s,'empty' => 'Please choose a  student','class'=>'form-control'));?>
            </div>
            <div class="form-group">
                <?php echo $this->Form->input('4.name', array('type'=>'select',"options"=>$student1s,'empty' => 'Please choose a  student','class'=>'form-control'));?>
            </div>




            <div class="form-group">
                <?php echo $this->Form->submit(__('Submit'), array('class'  => 'btn btn-primary')); ?>
            </div>

        <?php echo $this->Form->end() ?>

    </div><!-- end col md 12 -->
  </div><!-- end row -->
 </div>

找到我的问题的解决方案以避免数据重复,这 link 有帮助 -> Validating multiple fields with the same name。我所要做的就是把这段代码 ->array('validate' => 'true')<- 放在这一行
如果 ($this->Group->save($this->request->data),array('validate' => 'true')) {

全部完成,无需禁用所选列表值。