0

I was following a youtube tutorial on making a codeigniter college management system but i am stuck at a point.. when i try to submit the form on wamp server select form fields not showing requied error on form submit

i have checked the name fields of the form inputs matching the database table but still when the form is submitted without any fiilling any fields the role and gender select field errors doesnt show

here is my code controller

welcome.php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {


    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    
    public function index()
    {   
        //$this->load->helper('url');
        $this->load->view('home');
        
    }
    
    public function adminRegister()
    {
        $this->load->model('queries');
        $roles = $this->queries->getRoles();
        // print_r($roles);
        // exit();
        $this->load->view('register',['roles'=>$roles]);
    }
    
    public function adminLogin()
    {
        echo 'Login';
    }
    public function adminSignup()
    {
        //echo 'Registered succesfully';
        
        $this->form_validation->set_rules('username','Username','required');
        $this->form_validation->set_rules('email','Email','required');
        $this->form_validation->set_rules('gender','Gender','required');
        $this->form_validation->set_rules('role_id','Role','required');
        $this->form_validation->set_rules('password','Password','required');
        $this->form_validation->set_rules('confpwd','Password Again','required');
        $this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');
        
        if($this->form_validation->run()){
            echo 'validation passed';
        }else{
            //echo 'validation error';
            echo validation_errors();
        }
    }
    
    
}

and views register.php

<?php include('inc/header.php');?>
  <div class="container mt-2">
   <?php  echo form_open('welcome/adminSignup',['class'=>'form-hoizontal']);?>
       <h3 class="text-center display-4">ADMIN REGISTER</h3>
       <hr/>
    <div class="row">
    
       <div class="col-md-6">
       
           <div class="form-group">
             <div class="row">
                <div class="col-md-3">
                  <label for="username" class="mt-2">User name:</label>
                </div>
                 <div class="col-md-9">
                   <!-- <input type="text" class="form-control" placeholder="User name" id="username"> -->
                   <?php 
                   $data = array(
                    'type'  => 'text',
                    'name'  => 'username',
                    'placeholder' => 'Enter Username',
                    'class' => 'form-control'
                       );
                   echo form_input($data); ?>
                </div>
              </div>
            </div>  
            
         </div>
       
       <div class="col-md-6">
          <?php echo form_error('username','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    <div class="row">
    
       <div class="col-md-6">
       
              <div class="form-group">
              <div class="row mt-3">
                <div class="col-md-3">
                   <label for="email" class="mt-2">Email address:</label>
                </div>
                 <div class="col-md-9">
                    <!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
                     <?php 
                   $data = array(
                    'type'  => 'email',
                    'name'  => 'email',
                    'placeholder' => 'Enter Email',
                    'class' => 'form-control'
                       );
                   echo form_input($data); ?>
                 </div>
               </div>
            </div>  
            
         </div>
       
       <div class="col-md-6">
          <?php echo form_error('email','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    
    <div class="row">
    
       <div class="col-md-6">
       
                 <div class="form-group">
                  <div class="row mt-3">
                    <div class="col-md-3">
                       <label for="gender" class="mt-2">Gender:</label>
                    </div>
                     <div class="col-md-9">
                        <!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
                         <select class="form-control" name="gender">
                            <option>Select</option>
                            <option>Male</option>
                            <option>Female</option>
                            
                        </select>
                    </div>
                  </div>
              </div>
            
         </div>
       
       <div class="col-md-6">
          <?php echo form_error('gender','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    <div class="row">
    
       <div class="col-md-6">
       
           <div class="form-group">
              <div class="row mt-3">
                <div class="col-md-3">
                   <label for="email" class="mt-2">Role:</label>
                </div>
                 <div class="col-md-9">
                    <select class="form-control" name="role_id">
                    
                        <option>Select</option>
                    <?php if(count($roles)) { ?>
                        <?php foreach ($roles as $role){?>
                        <option><?php echo $role->rolename;?></option>
                        <?php }
                        } ?>
                    </select>
                </div>
              </div>
          </div> 
            
         </div>
       
       <div class="col-md-6">
           <?php echo form_error('role_id','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    <div class="row">
    
       <div class="col-md-6">
       
            <div class="form-group">  
              <div class="row mt-3">
                <div class="col-md-3">
                  <label for="password" class="mt-2">Password:</label>
                </div>
                 <div class="col-md-9">
                   <!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
                    <?php 
                   $data = array(
                    'type'  => 'password',
                    'name'  => 'password',
                    'placeholder' => 'Enter Password',
                    'class' => 'form-control'
                       );
                   echo form_input($data); ?>
                </div>
              </div>
           </div>
            
         </div>
       
       <div class="col-md-6">
          <?php echo form_error('password','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    <div class="row">
    
       <div class="col-md-6">
       
            <div class="form-group">  
              <div class="row mt-3">
                <div class="col-md-3">
                  <label for="password" class="mt-2">Password Again:</label>
                </div>
                 <div class="col-md-9">
                   <!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
                    <?php 
                   $data = array(
                    'type'  => 'password',
                    'name'  => 'confpwd',
                    'placeholder' => 'Enter Password Again',
                    'class' => 'form-control'
                       );
                   echo form_input($data); ?>
                </div>
              </div>
           </div>
            
         </div>
       
       <div class="col-md-6">
           <?php echo form_error('confpwd','<div class="text-danger">','</div>');?>
       </div>
       
    </div><!--row-->
    
    <div class="row">
     <div class="col-md-6">
            <!--  <button type="submit" class="btn btn-dark float-right">Register</button> -->
           <div class="float-right">
             <?php echo form_submit('Register', 'Register',"class='btn btn-dark'"); ?>
           <?php echo anchor('welcome','GO BACK',['class'=>'btn  btn-warning']);?>
           </div>
       </div>
       
    </div>
    
    
    <?php echo form_close(); ?>
  </div>
<?php include('inc/footer.php');?>

here is screenshot enter image description here

Sash_007
  • 23
  • 7

2 Answers2

1

The errors are not showing because your select boxes are always submitting a value. When you select an option in the selectbox and no value attribute is specified, the value after the <option> tag is being sent to the server.

To trigger the required rule, you need to send an empty string. To do this, you can either use an empty <option> tag for the placeholder:

<select class="form-control" name="gender">
    <option></option>
    <option>Male</option>
    <option>Female</option>
</select>

Or set the value attribute of the placeholder to an empty string:

<select class="form-control" name="gender">
    <option value="">Select</option>
    <option>Male</option>
    <option>Female</option>
</select>
Marleen
  • 2,245
  • 2
  • 13
  • 23
0

Try below Code:

<select class="form-control" name="gender">
    <option></option>
    <option value="male">Male</option>
    <option valie="female">Female</option>
</select>