Reputation: 106
I am currently having an issue with validation on dynamic forms in code igniter.
<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]">
Above is a part of my form. This form submits the data as an array. What I want to do is be able to use the codeigniter form validator to validate all the fields. The problem with this currently is that the form is dynamic. The front end allows these sets of inputs to be multiplied an infinite amount of times using javascript. Does anyone have any ideas on how I could solve this issue?
Upvotes: 1
Views: 406
Reputation: 358
You can use the below method...
//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");
}
Upvotes: 1
Reputation: 3053
Write your own validation function and handle the array however you'd like:
$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;
}
}
More in CodeIgniter documentation.
Upvotes: 0