Php 多维度表单数据的Codeigniter表单验证
我目前在CodeIgniter中对动态表单进行验证时遇到了一个问题Php 多维度表单数据的Codeigniter表单验证,php,codeigniter,Php,Codeigniter,我目前在CodeIgniter中对动态表单进行验证时遇到了一个问题 <input name="item1_name[0][PostTitle]"> <input name="item1_name[0][PostSubject]"> <input name="item1_name[0][PostMessage]"> <input name="item1_name[0][PostSlug]"> <input name="item1_name[1
<input name="item1_name[0][PostTitle]">
<input name="item1_name[0][PostSubject]">
<input name="item1_name[0][PostMessage]">
<input name="item1_name[0][PostSlug]">
<input name="item1_name[1][PostTitle]">
<input name="item1_name[1][PostSubject]">
<input name="item1_name[1][PostMessage]">
<input name="item1_name[1][PostSlug]">
以上是我表格的一部分。此表单以数组形式提交数据。我想做的是能够使用codeigniter表单验证器来验证所有字段。目前的问题是表单是动态的。前端允许使用javascript将这些输入集无限次地相乘。有人对我如何解决这个问题有什么想法吗?编写您自己的验证函数并以您喜欢的方式处理阵列:
$this->form_validation->set_rules('item1_name', 'Item Name', 'callback_item_name_check');
function item_name_check($value)
{
// evaluate $value and return TRUE or FALSE with error message
if ($value == 'test')
{
$this->form_validation->set_message('item_name_check', 'You need to enter something else.');
return FALSE;
}
else
{
return TRUE;
}
}
更多信息请参见。编写自己的验证函数,并按照自己的意愿处理阵列:
$this->form_validation->set_rules('item1_name', 'Item Name', 'callback_item_name_check');
function item_name_check($value)
{
// evaluate $value and return TRUE or FALSE with error message
if ($value == 'test')
{
$this->form_validation->set_message('item_name_check', 'You need to enter something else.');
return FALSE;
}
else
{
return TRUE;
}
}
更多信息请参见。您可以使用以下方法
//get the array
$item1_name = $this->input->post('item1_name', TRUE);
foreach ($item1_name as $key => $item1_name)
{
// Set the rules
$this->form_validation->set_rules($key."[PostTitle]", "PostTitle", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSlug]", "PostSlug", "trim|required");
}
你可以使用下面的方法
//get the array
$item1_name = $this->input->post('item1_name', TRUE);
foreach ($item1_name as $key => $item1_name)
{
// Set the rules
$this->form_validation->set_rules($key."[PostTitle]", "PostTitle", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSlug]", "PostSlug", "trim|required");
}
我相信在添加规则so
item1_name['.$key.][PostTitle]
时,您必须为输入使用完整名称。我相信在添加规则soitem1_name['.$key.][PostTitle]
时,您必须为输入使用完整名称