4

I'm having a weird problem with the form_validation module of code igniter. I'm trying to validate multi dimensional arrays from the form post, but its not working as expected. I've used this a hundred times (exaggeration) with standard form posts so I'm familiar with it.

My form post looks like this

Array
(
    [location_edit_id] =>
    [theImage] => 
    [thePDF] => 
    [loc] => Array
    (
        [name] => 
        [content_1] => 
        [content_2] => 
        [opening_hours] => 
        [seats] =>
    )
    [ad] => Array
    (
        [address_1] => 
        [address_2] => 
        [address_3] => 
        [town_city] => 
        [county_id] =>
        [region_id] =>
        [postcode] => 
        [telephone] => 
        [email] => 
    )
 )

According to the docs - the action in my controller needs to look like this if I want to validate the $_POST['loc']['name']

$this->validation->set_rules( 'loc[name]', 'Location Name', 'required');

if ($this->validation->run() == FALSE)
{
    die( "did not validate" );
} 
else
{
    die( "validated" );
}

no matter what I do, this always validates even if $_POST['loc']['name'] is empty. I've examined the library file libraries/Validation.php and I cant see anywhere where this would actually work (as its always just looking for variable name matches - not arrays), so I'm not sure whats going on.

EDIT: I'm using Code igniter version 1.7.2 which is the latest stable release.

2 Answers 2

2

It looks like you're using the wrong library. The Validation library is deprecated. Try using Form_validation (libraries/form_validation.php) instead.

$this->load->library('form_validation');

$this->form_validation->set_rules( 'loc[name]', 'Location Name', 'required');

if ($this->form_validation->run() == FALSE)
{
    die( "did not validate" );
}
else
{
    die( "validated" );
}
Sign up to request clarification or add additional context in comments.

Comments

0

i am not sure about the latest CI versions but back in 1.6 days this wasn't possible .. what version of CI are u using?

I used to use this back then

http://codeigniter.com/wiki/Assosiative_Arrays_via_POST/

2 Comments

I'm using version 1.7.2 - so I'm stumped
i know that one is for 1.7.1 but i guess if they have left it open then there has to be a reason?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.