Welcome Guest, Not a member yet? Register   Sign In
Iterate through post values
#1

[eluser]xzela[/eluser]
Hello,

This may have been answered else where but I'll ask anyway.

Is there a recursive solution that iterates through post values while using the Form_Validation library?

Let's suppose that you have a form which has ten fields:
Code:
$this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[1]|max_length[64]');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[1]|max_length[64]');
$this->form_validation->set_rules('spouse_first', 'Spouse First Name', 'trim|max_length[64]');
$this->form_validation->set_rules('spouse_last', 'Spouse Last Name', 'trim|max_length[64]');
$this->form_validation->set_rules('home_phone', 'Home Phone', 'trim|max_length[20]');
//etc...

When the validation is has been satisfied and you are ready to insert the post values into a database you must manually insert each field into an array (or at least that's what I do) before sending the post values:

Code:
$fields = array();
$fields['first_name'] = set_value('first_name');
$fields['last_name'] = set_value('last_name');
$fields['spouse_first'] = set_value('spouse_first');
$fields['spouse_first'] = set_value('spouse_first');
$fields['home_phone'] = set_value('home_phone');
//etc...
$this->customer_model->insertCustomer($fields);
The above solution works but i can be bothersome if you have lots of fields to contend with. The solution I am looking for needs to be more elegant than the above example. What I would like is something that can iterate through the list of the post values and populate an array. Maybe something like this:
Code:
$fields = array();
foreach($this->form_validation->list_post_values() as $field) {
$fields[$field[name]] = $field[value];
}
$this->customer_model->insertCustomer($fields);

Does anyone have an idea on how I might go about this?

Let me know what you think.

Thanks
#2

[eluser]TheFuzzy0ne[/eluser]
First of all, you don't need to use set_value, as it's for forms. Generally, you should use $this->input->post('key') or $_POST['key'] (I prefer the first). You have two options that I can see.

Option 1:

Pass $_POST straight to your model, and have your model "extract" the required data.

Option 2:

You could extend the input library:

./system/application/libraries/MY_Input.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Input extends CI_Input {
      
    function _fetch_from_array(&$array, $index = '', $xss_clean = FALSE)
    {
        if ($index === '')
        {
            $arr = array();
            
            foreach ($array as $key => $val)
            {
                $arr[$key] = ($xss_clean === TRUE && ! $this->use_xss_clean) ? $this->xss_clean($val) : $val;
            }
            
            return $arr;
        }
        
        else if (is_array($index))
        {
            $arr = array();
            
            foreach ($index as $key)
            {
                if (isset($array[$key]))
                {
                    $arr[$key] = ($xss_clean === TRUE && ! $this->use_xss_clean) ? $this->xss_clean($array[$key]) : $array[$key];
                }
                else
                {
                    $arr[$key] = FALSE;
                }
            }
            
            return $arr;
        }
        
        else if ( ! isset($array[$index]))
        {
            return FALSE;
        }
        
        if ($xss_clean === TRUE)
        {
            return $this->xss_clean($array[$index]);
        }

        return $array[$index];
    }
}

/* End of file MY_Input.php */
/* Location: ./application/libraries/MY_Input.php */

This will allow you to do something like this:

$data = $this->input->post(array('first_name', 'last_name', spouse_first', 'spouse_last', 'home_phone'));

Not specifying an argument will return the entire $_POST array (even though you don't have to use that, as you can just use $_POST.
#3

[eluser]Pascal Kriete[/eluser]
The form validation library rebuilds the post array with the new values, so you just need to make sure that it doesn't contain any invalid keys. I usually do something like this post-validation:
Code:
$allowed = array('name', 'email', 'location');
$allowed = array_flip($allowed);
$data = array_intersect_key($_POST, $allowed);

$this->customer_model->insertCustomer($data);

[Edit: Apparently I'm too slow Wink ]
#4

[eluser]xzela[/eluser]
I think I may have come up with a solution with the help of your input:

Check it out:

Code:
$fields = array();
//create an array of keys from the post
$keys = array_keys($_POST);
foreach($keys as $key) {
    //If the value of the input is not "", insert that input into the fields array
    if($this->input->post($key) != "") {
        $fields[$key] = $this->input->post($key);    
    }
}
//print out fields
print_r($fields);

Let me know what you guys think
#5

[eluser]TheFuzzy0ne[/eluser]
That can break if someone submit's their own form with extra data.

EDIT: You'll also end up with the submit button being in there somewhere.
#6

[eluser]xzela[/eluser]
How so?
I am not sure what you mean.
Do you have an example?
BTW, I do like your approach to this problem. Extending the input class seems to be the best solution.
#7

[eluser]artie11[/eluser]
Code:
$fields = array('Display','Salutation','FirstName','LastName','Phone','Company','Mobile');
$keys = array_keys($_POST);
foreach($keys as $key) {
    if(in_array($key,$fields)) {
    $data[$key] = $this->input->post($key,TRUE);    
    }
}

Here's Your best option, Only Adds fields that you specify and still allow variable number of fields to be sent.

Only updated fields for instance, from a mobile device




Theme © iAndrew 2016 - Forum software by © MyBB