Dropdown required not working in codeigniter

2019-09-19 03:52发布

问题:

I want to make the mail is field required, but somehow its not working, am i missing something?

<div class="col-md-5 ">
  <div class="control-group form-group">
    <label class="control-label">
      Escalation Email Ids
    </label>
    <div class="controls">
      <?php $options=a rray(); if(count($useridsoptions)){ foreach($useridsoptions as $key=>$val){ $options[$key]=$val; } } echo form_dropdown('esc_users[]', $options, explode(",",$row->esc_users),'id="esc_users" class="form-control function col-md-12 select2", reqired="required" multiple'); ?>
    </div>
  </div>
</div>

回答1:

Noticed several issues in code. required & array spelling is wrong moreover wrong syntax used in last params in form_dropdown() function

Try this

<div class="col-md-5 ">
  <div class="control-group form-group">
    <label class="control-label">
      Escalation Email Ids
    </label>
    <div class="controls">
      <?php $options=array(); if(count($useridsoptions)){ foreach($useridsoptions as $key=>$val){ $options[$key]=$val; } } echo form_dropdown('esc_users[]', $options, explode(",",$row->esc_users),'id="esc_users" class="form-control function col-md-12 select2" required="required" multiple'); ?>
    </div>
  </div>
</div>


回答2:

Codeigniter dropdown validation example

<?php  echo form_label("Country:
","country");
     $data = array(
          "selectcountry"  => "Select Country",               
          "CA" => "Canada",
          "US" => "United States",
          "ZW" => "Zimbabwe"
                  );
     echo form_dropdown('country', $data, 'selectcountry');?>

form validation rule

<?php  $this->form_validation->set_rules('country', 'Country', 'required|callback_country_check');?>

callback method

 public function country_check()
    {
            if ($this->input->post('country') === 'selectcountry')  {
            $this->form_validation->set_message('country_check', 'Please choose your country.');
            return FALSE;
        }
        else {
            return TRUE;
        }
    }