• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
validating multi-dimensional arrays

#1
[eluser]datdesignguy[/eluser]
Hi all, I'm in a bit of a pickle. It should be pretty easy to answer, but I'm a little rusty and can't seem to find the answer I'm looking for.

I have a form, that allows the user to add up to 8 contact records to an application. I implemented this on the frontend with javascript, and used numbered arrays to post the data to the backend.

So, if there are 3 contact records being submitted in the form, then they would arrive into the backend as numbered arrays, 1, 2, and 3. each of those arrays have: name, title, company, email, address,phone, bio, admin_name, admin_email, admin_phone fields.

I'm trying to validate input for each of these contact records, by maintaining a hidden form field that keeps track of the id-numbers of each contact record added to the html form. So in the example above, if we have 3 contact records with id's 1,2,3, then the contents of the hidden field "cospeakers" would be: 1,2,3, (the last comma is chopped off by php in the code below)

The problem I'm having is actually two:

Somewhere validation is getting hung up on an array to string conversion (not sure where yet), and secondly is it possible to write valid rule-sets using the method I've employed in the code below (multi-dimensional rules)?

Any help you can provide on this subject is HIGHLY appreciated!

Thanks, -gregj

Code:
if($this->input->post('has_cospeaker') == 1){
    $cospkrs = explode(',',rtrim($this->input->post('cospeakers'),','));
    $i=1;
    foreach($cospkrs as $id) {
        $cospkr = $this->input->post($id);
        $rules[$id]['name'] = "required|min_length[3]";
        $rules[$id]['title'] = "required|min_length[3]";
        $rules[$id]['company'] = "required";
        $rules[$id]['email'] = "valid_email";
        $rules[$id]['phone'] = "min_length[10]";
        $rules[$id]['bio'] = "required|callback__bio_words";
        
        $fields[$id]['name'] = "Co-Speaker $i Name";
        $fields[$id]['title'] = "Co-Speaker $i Title";
        $fields[$id]['company'] = "Co-Speaker $i Company";
        $fields[$id]['email'] = "Co-Speaker $i Email";
        $fields[$id]['phone'] = "Co-Speaker $i Phone";
        $fields[$id]['bio'] = "Co-Speaker $i Bio";
        
        if($cospkr['hasadmin'] == 1) {
            $rules[$id]['admin_name'] = "required|min_length[3]";
            $rules[$id]['admin_email'] = "valid_email";
            $rules[$id]['admin_phone'] = "required|min_length[10]";
            
            $fields[$id]['admin_name'] = "Co-Speaker $i Admin Contact Name";
            $fields[$id]['admin_email'] = "Co-Speaker $i Admin Email";
            $fields[$id]['admin_phone'] = "Co-Speaker $i Admin Phone";
        }
        
        $cospkrs[$id] = $cospkr;
        $i++;

    }
    $data['cospkrs'] = $cospkrs;
    $data['last_cospkr'] = $id;
    unset($i,$cospkr);
}

#2
[eluser]jpcote[/eluser]
Hi,

I have the exact same problem.
Any advice from CI team ?

My form is a grid composed of 2 inputs (price and number), each line has his 2 inputs.

My post var is :
[price] => Array ( [0] => 1.20 [1] => 2.36 [2] => 2.33 [3] => 5.00)
[number] => Array ( [0] => 1 [1] => 2 [2] => 3)

So how to validate that price is currency (or at least numeric) and that number is integer ?

Thanks ! I hope it's clear enough... sorry if not Smile

#3
[eluser]datdesignguy[/eluser]
JP,

I ended up solving the problem by simply renaming my form elements. It's a lot easier to just dump the multi-dim array if possible, due to the way that CI provides access to the values of fields with errors. At least that's my experience. I'm still working with the 1.5.4 codebase, so I don't know if that's changed much in the 1.6.x releases.

Using a fairly simple javascript, my form dynamically adds additional fields to the form, so all I had to do for the validation phase was maintain a hidden field that keeps track of the id numbers of each field that javascript/dom adds to the form.

IE, the first contact item in my list would be:

contact_1_firstname, contact_1_lastname, etc.

then if the user adds a second contact field to the form:

contact_2_firstanme, contact_2_lastname, etc.

The unfortunate thing about this approach is when I use a foreach loop to re-display each of these fields on an error form, I have to create a list of the values like so:

$fname = "contact_$i_firstname";
$lname = "contact_$i_lastname";

So that the validation class can redisplay the class:

$this->validation->$fname;

This feels like a really sloppy solution to me, and I too am still waiting to hear from other Igniters to see what they think...

#4
[eluser]datdesignguy[/eluser]
Ok, so I should've double checked my code before I went with that solution. It didn't work.

#5
[eluser]jpcote[/eluser]
The only way to solve that was to not use the validation class anymore.

I do the validation in a foreach and I verify for every rules I need.

It's not as beautiful as the validation class but I works with my form...

Code:
$valid = true;
$error = '';

// valid_date
if(!strtotime($_POST['required_date'])) {
        $valid = false;
        $error .= 'Date is not valid.<br>';
}

for($i=0;$i<$numberoflines;$i++) {        
    // required|is_numeric|!is_empty
    if(isset($_POST['price_unit'][$i]) && (!is_numeric($_POST['price_unit'][$i]) || strcmp($_POST['price_unit'][$i],'')==0)) {
        $valid = false;
        $error .= 'The price of "'.$_POST['name'][$i].'" is not valid.<br>';
    }
    // required|is_numeric|!is_empty
    if(isset($_POST['number'][$i]) && (!is_numeric($_POST['number'][$i]) || strcmp($_POST['number'][$i],'')==0)) {
        $valid = false;
        $error .= 'The number of "'.$_POST['name'][$i].'" is not valid.<br>';
    }
}

if ($valid === FALSE)
{    
    //DISPLAY $ERROR
}
else
{
    //DO THE RIGHT THING
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.