如何为不同的单选按钮值设置表单验证

时间:2015-04-24 16:41:31

标签: forms codeigniter validation

我的表格有3个单选按钮用于我的注册表单 - 用户可以选择3个用户角色中的一个。如果您选择第一个单选按钮,您会看到以下下拉菜单:区域,学校,教师,班级和班级,对于单选按钮2 - 您会看到第一个下拉菜单,但没有老师。如果您选择第3个单选按钮,则会看到所有教师的复选框。 我根据用户选择显示和隐藏下拉列表。 如何进行表单验证 - 如果选择第一个单选按钮,则仅显示用于该单选按钮的表单验证规则。等三个单选按钮。我的控制器是:



public function register()
    {
        
        $this->form_validation->set_rules('first_name', 'First name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last name', 'trim|required'); 
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]|max_length[12]|is_unique[users.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]'); 
        $this->form_validation->set_rules('password2', 'Confirm password', 'trim|required|matches[password]');  
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('location', 'Location', 'trim|required');
        $this->form_validation->set_rules('school[]', 'School', 'required');
        $this->form_validation->set_rules('class[]', 'Class', 'required');
        $this->form_validation->set_rules('role_id', 'Role', 'required');
        $this->form_validation->set_rules('class_divisions[]', 'Class division', 'required');
        $this->form_validation->set_rules('region', 'Region', 'required');
        $this->form_validation->set_rules('teacher[]', 'Teacher', 'required');
        //$this->form_validation->set_rules('all_teachers_show', 'All teachers', 'required');
     
        if ($this->form_validation->run()==FALSE)
        {
            $this->signup();
        }
        else 
        {  
            if( $this->user_model->register())
            {
                $data['dynamic_view'] = 'success_reg'; 
                $this->load->view('templates/main',$data);
            }
            else
            {
                $this->load->model('user_model');   
                $data['dynamic_view'] = 'register_form'; 
                $data['regions'] = $this->user_model->regions_show();
                $data['classes'] = $this->user_model->classes_show();
                $data['school_show'] = $this->user_model->school_show();
                $data['class_divisions'] = $this->user_model->class_divisions_show();
                $data['all_teachers_show'] = $this->user_model->all_teachers_show();
                $this->load->view('templates/main',$data);
            }   
        }
    }




我的观点是:



 <!DOCTYPE html>
    <html>
      <head>   
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
        <script>

      $(document).ready(function(){
          $(":radio").click(function(){
              $('#region').val('Choose region');
              $('#school').val('Choose region');
              $('#teacher').val('Choose school');
              $('#class').val('Choose class');
              $('#class_divisions').val('Choose division');
          });
      });

          function showHide(self, show){

              $(".all_teachers_show, .toggle, .school,  .teacher_school, .teacher, .class, .teacher_class").hide();
     
              if (show)
                  $('.toggle').show();
              else
                  $('.toggle').hide();
              $(":radio").prop('checked',false);
              $(self).prop('checked',true);
          }
          
          function show(yes, no){
              if (no)
                  $('.school').show();
              else
                  $('.school').hide();
              $("region").prop('checked',false);
              $(yes).prop('checked',true);
          }
          
          function school_show(yes, no){
      
            if(document.getElementById("radio1").checked===true){
               document.getElementById('class_label').innerHTML = 'Class:*';
              if (no)
                    $('.teacher').show();
                else
                    $('.teacher').hide();
                $("school").prop('checked',false);
                $(yes).prop('checked',true);
            }
           
            if(document.getElementById("radio2").checked===true){
                document.getElementById('class_label').innerHTML = 'Class teacher:*';
              if (no)
                  $('.class').show();
              else
                  $('.class').hide();
              $("school").prop('checked',false);
              $(yes).prop('checked',true);

            }
          }
         
          function class_show(yes, no){
          
            if (no)
                  $('.class').show();
              else
                  $('.class').hide();
              $("teacher").prop('checked',false);
              $(yes).prop('checked',true);
          
          }
          
          function teachers_show(yes, no){
            $(".toggle, .all_teachers_show,  .school,  .teacher_school, .teacher, .class, .teacher_class").hide();
              if (no)
                  $('.all_teachers_show').show();
              else
                  $('.all_teachers_show').hide();
              $(":radio").prop('checked',false);
              $(yes).prop('checked',true);
          }
           
      </script>   
      <script>
            $(document).ready(function() {
                $('#region').change(function() {
                    var url = "<?= base_url() ?>index.php/home/get_schools";
                    var postdata = {region: $('#region').val()};
                    $.post(url, postdata, function(result) {
                        var $school_sel = $('#school');
                        $school_sel.empty();
                        $school_sel.append("<option>Choose region</option>");
                        var schools_obj = JSON.parse(result);
                        $.each(schools_obj, function(key, val) {
                            var option = '<option  value="' + val.school_id + '">' + val.school_name + '</option>';
                            $school_sel.append(option);
                        });
                    });
                });
            });
      </script>  
      <script>
            $(document).ready(function() {
                $('#school').change(function() {
                    var url = "<?= base_url() ?>index.php/home/get_teachers";
                    var postdata = {school: $('#school').val()};
                    $.post(url, postdata, function(result) {
                        var $teacher_sel = $('#teacher');
                        $teacher_sel.empty();
                        $teacher_sel.append("<option>Choose school</option>");
                        var teachers_obj = JSON.parse(result);
                        $.each(teachers_obj, function(key, val) {
                            var option = '<option value="' + val.user_id + '">' + val.username + '</option>';
                            $teacher_sel.append(option);
                        });
                    });
                });
            });
      </script>
      
      </head>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

控制器中的

检查发布的数据 -

    if ($this->input->post('radio_button') == 1){
        $this->form_validation->set_rules('min_length', 'Minimum Length', 'trim|required|max_length[3]|integer');
           //add more validation
    }
    if ($this->input->post('radio_button') == 2){
        $this->form_validation->set_rules('min_length', 'Minimum Length', 'trim|required|max_length[3]|integer');
           //add more validation
    }