现在,我有一个自定义函数,它将批量检查表单上的每个提交,以确保它符合一组规则。我认为将我的表单设置为 html 数组 (data[]),在代码点火器中检索它,然后使用 CI callback_ 验证功能确保一切正常。它看起来很复杂,所以我还没有完全理解它,但也许这可以让你的轮子转向正确的方向。
编辑:
$this->load->library('form_validation');
// If there is any posted data, then we should assign it to our $post_data array.
$post_data = $this->input->post('project_data');
if (empty($post_data)) {die('empty form');}
// Now, we are ready to validate the incoming data.
// We will send the data through a callback function which will check to make sure it is valid.
// If it is not valid, the callback function will trigger a codeigniter validation error.
// Let's temporarily remove any commas from the submission data to avoid delimiter confusion when sending it through the callback
$post_data = str_replace(",", "DELIMITEDCOMMA", $post_data);
$post_data_str = http_build_query($post_data);
$this->form_validation->set_rules("project_data[errors]", 'Errors', "required|callback__validate_project_data[$post_data_str]");
$this->form_validation->run();
然后,只需根据您需要验证的内容编写自定义验证函数。
function _validate_project_data($value, $request)
{
// A callback rule check is being attempted by the CI validator
// $value is the actual value of the submission, while $request is the key and value
$request = explode(",", $request);
$request = str_replace("DELIMITEDCOMMA", ",", $request);
// rename the keys in the request back to the original convention
parse_str($request[0], $request);
//var_dump($request);
// perform validation here and return true or false (valid or invalid)
}