我的表格有以下结构:
Field A: <input name="something[34]"> -- Field B: <input name="something_else[11]">
Field A: <input name="something[93]"> -- Field B: <input name="something_else[67]">
Field A: <input name="something[5]"> -- Field B: <input name="something_else[212]">
...
...
等等,里面有未定义的行数和索引值。
我无法弄清楚如何验证。我正在尝试不同的方式,但我猜不出来:
$this->form_validation->set_rules('something[]', 'Something', 'required|xss_clean');
$this->form_validation->set_rules($_POST[something[]], 'Something', 'required|xss_clean');
$this->form_validation->set_rules($_POST[something][], 'Something', 'required|xss_clean');
依旧......
我无法弄清楚如何根据表单验证部分的文档来管理它。你能帮我个忙吗?提前谢谢!
我使用foreach将数据保存回数据库:
foreach ($_POST['something'] as $something_id => $something_name) {
if ($this->form_validation->run() === TRUE) {
//insert
}
}
答案 0 :(得分:5)
您可以通过多种方式执行此操作,您可以遍历帖子项目数组并在每个帖子项目上添加规则(取决于您需要何种类型的验证)
$this->form_validation->set_rules('something[]', 'Something', 'required|xss_clean'); // Ensure something was provided
if (is_array($_POST['something'])) {
foreach ($key as $key => $value) {
$this->form_validation->set_rules('something['.$key.']', 'Something', 'required|greater_than[10]|xss_clean'); // Any validation you need
}
}
您也可以编码your own custom validation,因为Codeigniter允许它:
$this->form_validation->set_rules('something[]', 'Something', 'callback_something_check');
...
function something_check($something) {
// Do what you need to validate here
return true; // or false, depending if your validation succeeded or not
}