如何在codeigniter中将数组的键验证为表单数据。
我像下面这样使用它,但它不起作用。
$this->form_validation->set_rules("general[FirstName]", "Name", "trim|required|max_length[100]");
$this->form_validation->set_rules("contact[Email]", "Email", "trim|required|valid_email");
$this->form_validation->set_rules("contact[Phone]", "Phone", "trim|xss_clean|max_length[15]");
$this->form_validation->set_rules("contact[AlternatePhone]", "AlternatePhone", "trim|xss_clean|max_length[15]");
$this->form_validation->set_rules("contact[AddressLine1]", "AddressLine1", "trim|xss_clean|required");
请提出建议。
$this->form_validation->set_rules('contact[0][Email]', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules("contact[1][Phone]", "Phone", "trim|xss_clean|max_length[15]");
如果您的请求数组如下所示,则此示例将起作用:-
"contact":[{
"Email":"test@example.com"
},
{
"Phone":"9870231645"
}]
您可以迭代数组,并动态地对每个字段进行验证,而不是使用硬代码索引。