【问题标题】:Codeigniter "Unable to find validation rules" when using $this->form_validation->set_rules使用 $this->form_validation->set_rules 时 Codeigniter “找不到验证规则”
【发布时间】:2021-12-08 07:57:32
【问题描述】:

当我使用 $this->form_validation->set_rules 在控制器中设置规则时,我无法弄清楚为什么我在 php 日志中收到错误“无法找到验证规则”。

这是我在视图中的代码:

<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>

还有我在控制器中的 php 代码:

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>    

感谢您的帮助!如果您还有其他问题,请告诉我

【问题讨论】:

  • 您的意见是什么?我尝试使用您的代码,但没有显示该错误?

标签: php codeigniter codeigniter-3


【解决方案1】:

为什么不调用表单中的name值进行验证

<div class="form-group">
       <label for="yearList">Study Years</label>
       <input type="text" class="form-control" id="yearList" name="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" name="yearbox">
       </div>
`
$this->form_validation->set_rules('yearlist', 'Start Year', 'required',
                    array('required' => 'Start year must have a valid year.'));
            $this->form_validation->set_rules('yearbox', 'End Year', 'required',
                    array('required' => 'End year must have a valid year.'));

【讨论】:

    【解决方案2】:

    当您在帖子中发送参数时,您的参数周围有单引号。在验证规则中,您正在检查不带单引号的参数,只需将 set_rule 更改如下。

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

    还检查您的 ajax 请求参数是如何发送到服务器的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多