【问题标题】:How to set form validation for different values of radio buttons如何为单选按钮的不同值设置表单验证
【发布时间】:2015-04-24 16:41:30
【问题描述】:

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

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>

【问题讨论】:

    标签: forms codeigniter validation


    【解决方案1】:

    在您的控制器中检查发布的数据,如下所示-

        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
        }
    

    【讨论】:

    • 我试过这个,但它没有显示任何验证错误。我用过: if ($this->input->post('role_id[]') == 1){ $this->form_validation->set_rules('first_name', 'name', 'trim|required'); $this->form_validation->set_rules('last_name', 'name', 'trim|required'); //等等 } 我也试过 ($this->input->post('role_id') == 1){ 但没有错误
    • 您是否测试过您的表单提交到您的控制器功能?
    • 我的表单验证工作并显示错误。但是现在我想添加是否选中了名为 role_id 且值为 1、2 或 5 的明确单选按钮,然后仅向我显示这些下拉列表的这些验证错误,而不是全部。 if ($this->input->post('role_id') == 1){ 的问题是问题,这是访问它的方式 - 我的收音机被命名为 role_id[] ? :)
    • 你能改变你的单选按钮名称格式,并把这三个单选按钮组同名但肯定有不同的值吗?
    • 我将它们命名为 role_id[] 并且值为 1、2 或 5。我使用 var_dump($this->input->post('role_id')); 对其进行了测试。它向我显示了我选择的这个无线电值,但为什么验证不起作用?而且我也无法为所有下拉菜单和单选按钮设置值 - 当表单提交时不必再次选择。它适用于文本输入,但对于下拉菜单和收音机我做不到。查看我的代码;)这可能是因为我隐藏了它们并使用了 jQuery 吗?
    猜你喜欢
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 2013-12-04
    • 1970-01-01
    • 2012-10-10
    • 1970-01-01
    相关资源
    最近更新 更多