Welcome Guest, Not a member yet? Register   Sign In
Validate input of two possible arrays

(This post was last modified: 01-28-2020, 03:39 PM by dwlamb.)

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>
        <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>
        <input type="hidden" name="group_id" value="3">
        <input type="submit" name="submit" value="Submit"  />

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:

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

(This post was last modified: 01-29-2020, 11:21 AM by jreklund.)

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');



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?!'); 

Theme © iAndrew 2016 - Forum software by © MyBB