Search code examples
phpcodeignitercodeigniter-3

Codeigniter "Unable to find validation rules" when using $this->form_validation->set_rules


I can't figure out why I'm getting the error "Unable to find validation rules" in the php logs when I set the rules in the controller using $this->form_validation->set_rules.

Here is my code in the view:

<div id="yearDiv" class="form-group">
   <div class="form-group">
       <label for="yearList">Study Years</label>
       <input type="text" class="form-control" id="yearList">
    </div> 
    <div class="form-group row">
       <div class="col-md-2">
           <label for="startYearBox">Start Year</label>
           <input type="text" class="form-control" id="startYearBox">
       </div>
       <div class="col-md-2">
           <label for="endYearBox">End Year</label>
           <input type="text" class="form-control" id="endYearBox">
       </div> 
       <div class="col-md-8"><small>(Use present for End Year if study is ongoing.)</small>
       </div>
   </div>                
</div> 

<script type='text/javascript'>
var data = {};
data['years']={};

data['years']['intStartYr']= $('#startYearBox').val();
data['years']['intEndYr']= $('#endYearBox').val();
           
var posturl = '<?php echo site_url('manage/climate_indicators/updateStudyYears');?>'
         
$.when($.ajax({type: "POST", url: posturl, data:data, dataType: 'json'})).done(function(result) {
     console.log(result.status);
});
</script>

And my php code in the controller:

public function updateStudyYears() {
        
        if (!$this->input->post() OR !$this->input->is_ajax_request() ){
            echo json_encode(array('status'=>'failed','errors'=>array('Access Mode'=>'Disallowed')));
            exit();
        }
        else {
            $postData = $this->input->post(NULL, true);
            
            $yearArray = $postData['years'];
            
            if(empty($yearArray)) {
                echo json_encode(array('status'=>'failed', 'errors'=>'Years are empty'));
                exit();
            }    
            $this->form_validation->set_rules('years[intStartYr]', 'Start Year', 'required',
                    array('required' => 'Start year must have a valid year.'));
            $this->form_validation->set_rules('years[intEndYr]', 'End Year', 'required',
                    array('required' => 'End year must have a valid year.'));
            
            if ($this->form_validation->run() === FALSE) {  
                $validationErrs = $this->form_validation->error_array();
            
                echo json_encode(array('status'=>'form-errors', 'valErrs'=>$validationErrs));                
                exit;
            }
            else {                        
                $status = $this->climate_indicators_model->setStudyYears($yearArray);            
                if ($status) {
                    echo json_encode(array('status'=>'success', 'message'=>'Study year information successfully updated.'));                    
                }
                else echo json_encode(array('status'=>'failed', 'errors'=>'Db error on update'));
            }
            exit();
        }
    }
</script>    

Thank you for your help! Let me know if you have any further questions


Solution

  • When you are sending parameter in post it has single quotes around your parameter. In validation rule you are checking parameter without single quotes just change the set_rule as below.

    $this->form_validation->set_rules("years['intStartYr']", 'Start Year', 'required',
                        array('required' => 'Start year must have a valid year.'));
    

    also inspect your ajax request how parameters are going to server.