使用 $this->form_validation->set_rules 时,Codeigniter "Unable to find validation rules"
Codeigniter "Unable to find validation rules" when using $this->form_validation->set_rules
当我使用 $this->[=23= 在控制器中设置规则时,我无法弄清楚为什么 php 日志中出现“无法找到验证规则”错误]>set_rules.
这是我在视图中的代码:
<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>
感谢您的帮助!如果您还有其他问题,请告诉我
当您在 post 中发送参数时,它在您的参数周围有单引号。在验证规则中,您正在检查没有单引号的参数,只需更改 set_rule 如下。
$this->form_validation->set_rules("years['intStartYr']", 'Start Year', 'required',
array('required' => 'Start year must have a valid year.'));
还要检查您的 ajax 请求参数如何发送到服务器。
为什么不调用表单中的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.'));
当我使用 $this->[=23= 在控制器中设置规则时,我无法弄清楚为什么 php 日志中出现“无法找到验证规则”错误]>set_rules.
这是我在视图中的代码:
<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>
感谢您的帮助!如果您还有其他问题,请告诉我
当您在 post 中发送参数时,它在您的参数周围有单引号。在验证规则中,您正在检查没有单引号的参数,只需更改 set_rule 如下。
$this->form_validation->set_rules("years['intStartYr']", 'Start Year', 'required',
array('required' => 'Start year must have a valid year.'));
还要检查您的 ajax 请求参数如何发送到服务器。
为什么不调用表单中的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.'));