无法识别Codeigniter下拉验证

时间:2018-08-16 18:26:04

标签: php codeigniter

我对Codeigniter来说还很陌生,需要创建一个简单的下拉菜单,以识别单击“提交”后未选择的选项。我的下拉菜单工作正常,但验证无效(未选中选项时无法识别)。我使用了form_validation,但是我的代码似乎找不到问题。您能否提供有关如何解决此问题的见解?谢谢!

控制器(User.php):

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {

  public function index(){
    // load base_url 
    $this->load->helper('url');
    $this->load->library('form_validation');

    //load model 
    $this->load->model('Main_model');

    // get cities 
    $data['cities'] = $this->Main_model->getCity();
    $data['datasets'] = $this->Main_model->getDataset();

    $this->form_validation->set_rules('cities', 'Tool', 'callback_validate_dropdown');

    if ($this->form_validation->run() == FALSE)
        {
            // failed validation
            $this->load->view('user_view', $data);
        }
        else
        {
            // here goes your code to insert into db
            echo "Success";
        }

    // load view 


  }


  function validate_dropdown($selectValue)
    {
        if ($selectValue == '-Select-')
        {
            $this->form_validation->set_message('validate_dropdown', 'Please choose a valid %s');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

  public function getCityDepartment(){ 
    // POST data 
    $postData = $this->input->post();

    // load model 
    $this->load->model('Main_model');

    // get data 
    $data = $this->Main_model->getCityDepartment($postData);
    echo json_encode($data); 
  }

  public function getDepartmentUsers(){ 
    // POST data 
    $postData = $this->input->post();

    // load model 
    $this->load->model('Main_model');

    // get data 
    $data = $this->Main_model->getDepartmentUsers($postData);
    echo json_encode($data); 
  }


  public function getDatasetVersion(){ 
    // POST data 
    $postData = $this->input->post();

    // load model 
    $this->load->model('Main_model');

    // get data 
    $data = $this->Main_model->getDatasetVersion($postData);
    echo json_encode($data); 
  }



  public function getVersionUsers(){ 
    // POST data 
    $postData = $this->input->post();

    // load model 
    $this->load->model('Main_model');

    // get data 
    $data = $this->Main_model->getVersionUsers($postData);
    echo json_encode($data); 
  }


}

模型(Main_model.php):

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main_model extends CI_Model {

  // Get cities
  function getCity(){

    $response = array();

    // Select record
    $this->db->select('*');
    $q = $this->db->get('city');
    $response = $q->result_array();

    return $response;
  }

  // Get Datasets
  function getDataset(){

    $response = array();

    // Select record
    $this->db->select('*');
    $q = $this->db->get('dataset');
    $response = $q->result_array();

    return $response;
  }


  // Get City departments
  function getCityDepartment($postData){
    $response = array();

    // Select record
    $this->db->select('id,depart_name');
    $this->db->where('city', $postData['city']);
    $q = $this->db->get('department');
    $response = $q->result_array();

    return $response;
  }

  // Get Department user
  function getDepartmentUsers($postData){
    $response = array();

    // Select record
    $this->db->select('id,name,email');
    $this->db->where('department', $postData['department']);
    $q = $this->db->get('user_depart');
    $response = $q->result_array();

    return $response;
  }


  // Get Dataset Version's
  function getDatasetVersion($postData){
    $response = array();

    // Select record
    $this->db->select('id,version_name');
    $this->db->where('dataset', $postData['dataset']);
    $q = $this->db->get('dsversion');
    $response = $q->result_array();

    return $response;
  }


  // Get Version User JSON
  function getVersionUsers($postData){
    $response = array();

    // Select record
    $this->db->select('id,name');
    $this->db->where('dsversion', $postData['dsversion']);
    $q = $this->db->get('user_dsversion');
    $response = $q->result_array();

    return $response;
  }



}

查看(user_view.php)

<!doctype html>
<html>
<body>

   <table border='0'>


     <!-- City -->
     <tr>
       <td>Tool</td>
       <td>
         <select id='sel_city'>
           <option>-Select-</option>
           <?php
           foreach($cities as $city){
             echo "<option value='".$city['id']."'>".$city['cityname']."</option>";

           }
           ?>
        </select>
      </td>
    </tr>



    <!-- Department -->
    <tr>
      <td>Tool Version</td>
      <td>
        <select id='sel_depart'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>

    <!-- User -->
    <tr>
      <td>Corresponding Tool JSON Sent To Pachyderm</td>
      <td>
        <select id='sel_user'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>



    <!-- Dataset -->
     <tr>
       <td>Dataset</td>
       <td>
         <select id='sel_dataset'>
           <option>-Select-</option>
           <?php
           foreach($datasets as $dataset){
             echo "<option value='".$dataset['id']."'>".$dataset['datasetname']."</option>";
           }
           ?>
        </select>
      </td>
    </tr>


    <!-- Dataset Version -->
    <tr>
      <td>Dataset Version</td>
      <td>
        <select id='sel_dsversion'>
          <option>-Select-</option>
        </select>
      </td>
    </tr>


    <!-- Version User JSON -->
    <tr>
      <td>Corresponding Dataset JSON's Sent To Pachyderm</td>
      <td>
        <select id='sel_user_version'>
          <option> -Select- </option>
        </select>
      </td>
    </tr>
  </table>

  <!-- Script -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <script type='text/javascript'>
  // baseURL variable
  var baseURL= "<?php echo base_url();?>";

  $(document).ready(function(){

    // City change
    $('#sel_city').change(function(){
      var city = $(this).val();

      // AJAX request
      $.ajax({
        url:'<?=base_url()?>index.php/User/getCityDepartment',
        method: 'post',
        data: {city: city},
        dataType: 'json',
        success: function(response){

          // Remove options 
          $('#sel_user').find('option').not(':first').remove();
          $('#sel_depart').find('option').not(':first').remove();

          // Add options
          $.each(response,function(index,data){
             $('#sel_depart').append('<option value="'+data['id']+'">'+data['depart_name']+'</option>');
          });
        }
     });
   });



    // Dataset change
    $('#sel_dataset').change(function(){
      var dataset = $(this).val();

      // AJAX request
      $.ajax({
        url:'<?=base_url()?>index.php/User/getDatasetVersion',
        method: 'post',
        data: {dataset: dataset},
        dataType: 'json',
        success: function(response){

          // Remove options 
          $('#sel_user_version').find('option').not(':first').remove();
          $('#sel_dsversion').find('option').not(':first').remove();

          // Add options
          $.each(response,function(index,data){
             $('#sel_dsversion').append('<option value="'+data['id']+'">'+data['version_name']+'</option>');
          });
        }
     });
   });



    // Department change
   $('#sel_dsversion').change(function(){
     var dsversion = $(this).val();

     // AJAX request
     $.ajax({
       url:'<?=base_url()?>index.php/User/getVersionUsers',
       method: 'post',
       data: {dsversion: dsversion},
       dataType: 'json',
       success: function(response){

         // Remove options
         $('#sel_user_version').find('option').not(':first').remove();

         // Add options
         $.each(response,function(index,data){
           $('#sel_user_version').append('<option value="'+data['id']+'">'+data['name']+'</option>');
         });
       }
    });
  });





   // Department change
   $('#sel_depart').change(function(){
     var department = $(this).val();

     // AJAX request
     $.ajax({
       url:'<?=base_url()?>index.php/User/getDepartmentUsers',
       method: 'post',
       data: {department: department},
       dataType: 'json',
       success: function(response){

         // Remove options
         $('#sel_user').find('option').not(':first').remove();

         // Add options
         $.each(response,function(index,data){
           $('#sel_user').append('<option value="'+data['id']+'">'+data['name']+'</option>');
         });
       }
    });
  });



 });
 </script>





<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>CodeIgniter Dropdown from Database Example</title>
    <link href="<?php echo base_url("assets/bootstrap/css/bootstrap.css"); ?>" rel="stylesheet" type="text/css" />
    <style>
    .well {
        background: none;
    }
    </style>
</head>

<body>


    <div class="col-xs-6 col-xs-offset-3 well">
        <?php $attributes = array("name" => "booksform");
        echo form_open("User/index", $attributes);?>

        <div class="form-group">
            <button name="submit" type="submit" class="btn btn-info">Submit</button>

        </div>
        <?php echo form_close(); ?>
    </div>
</div>
</div>

</body>



 </body>
</html>

1 个答案:

答案 0 :(得分:0)

在用户控制器中替换

--> Scaling up dev3-2 from 0 to 1, scaling down dev3-1 from 1 to 0 (keep 1 pods available, don't exceed 2 pods)
    Scaling dev3-2 up to 1
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-p6mlq" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-rcwxc" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-m667b" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-j28gz" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-dwsz5" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-2xrvz" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-hwk8k" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-hrjk8" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 Error creating: pods "dev3-2-8lts5" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
-->  FailedCreate: dev3-2 (combined from similar events): Error creating: pods "dev3-2-74xzp" is forbidden: exceeded quota: compute-resources, requested: limits.cpu=1,limits.memory=512Mi, used: limits.cpu=2,limits.memory=1Gi, limited: limits.cpu=2,limits.memory=1Gi
error: timed out waiting for "dev3-2" to be synced

$this->form_validation->set_rules('cities', 'Tool', 'callback_validate_dropdown');

替换为视图

$this->form_validation->set_rules('sel_city', 'Tool', 'required');

<select id='sel_city'>
       <option>-Select-</option>

看看验证是否正常