CodeIgniter Forums
Validate input of two possible arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Validate input of two possible arrays (/showthread.php?tid=75346)



Validate input of two possible arrays - dwlamb - 01-28-2020

The code below depicts a form for which I am trying to create validation rules.  The form is for adding or removing users from a group. 

PHP Code:
    <form action="http://some_site.com/form_edit_group" method="post" accept-charset="utf-8">
        <p>Add to group.  Hold Ctrl-key and click for selecting more than one.</p>
        <select name="add_to_group[]" class="add_to_group" multiple size="4">
            <option value="3">Jane Smith</option>
            <option value="6">Jim Jones</option>
            <option value="7">Casey Jones</option>
        </select>
        <p>Existing members of groupTo remove hold Ctrl-key and click for selecting more than one:</p>
        <select name="remove_from_group[]" class="remove_from_group" multiple size="4">
            <option value="1">Bill Clinton</option>
            <option value="4">James Carter</option>
            <option value="5">John Kennedy</option>
        </select>
        <input type="hidden" name="group_id" value="3">
        <input type="submit" name="submit" value="Submit"  />
    </form

It is possible after clicking submit, $_post will have an array "add_to_group[]", "remove_from_group[]", both arrays or neither array in the event the administrator forgets to click on a user in either select list.
The validation rules I wish to set are:
  • whether $_post contains "add_to_group[]" or "remove_from_group[]"
  • if one or both of those arrays exist, perform checks on the data within the arrays <- these I have figured what to do
Looking through the documentation for Form Validation, I think the answer is a custom callback put can't figure out how to submit $_post in the rule.  A form validation rule holds a key within $_post:

PHP Code:
$this->form_validation->set_rules('username''Username''callback_username_check'); 


such as 'username'.  I want a rule with callback function that will examine $_post to see if either array exists.  Is there another method?


RE: Validate input of two possible arrays - jreklund - 01-29-2020

You can set an custom callback on whatever field you want. For e.g. on the group_id. And in that custom callback grab both arrays and do your checks. Post are always available (in controller and model).

PHP Code:
public function check_groups()
{
    
$add_to_group $this->input->post('add_to_group');
    
$remove_from_group $this->input->post('remove_from_group');

    if(
$checks_valid)
         return 
true;

    return 
false;


And depending on what fails, send custom error codes.
PHP Code:
$this->form_validation->set_message('check_groups''Add to group error');
$this->form_validation->set_message('check_groups''Remove to group error');
$this->form_validation->set_message('check_groups''No group selected?!');