Welcome Guest, Not a member yet? Register   Sign In
Multidimensional Array Validation
#1

Hi everyone, I need some help with Form_validation library.

I need to ensure posted data has one valid url. The post structure is

Code:
{
   "name": "Company Name",
   "emails": [
                   {"address": "[email protected]", "type":"main"},
                   {"address": "[email protected]", "type":"contact"},
                   .....
                   {"address": "[email protected]", "type":"suggest"}
                  ]
}

I need to validate this data and asure all emails have a valid email address format.

I tried this

Code:
foreach ($data["emails"] as $key => $email)
{
         $this->form_validation->set_rules(array("field" => "emails[{$key}]['address']", "label" => "email", "rules" => "valid_email");
}

and this

Code:
         $this->form_validation->set_rules(array("field" => "emails[]['address']", "label" => "email", "rules" => "valid_email");

But none of these options works,

Any help will be welcome.
Thanks in advance.
Reply
#2

(This post was last modified: 11-12-2015, 04:30 PM by cartalot.)

so this is one way of doing it, note that you are just doing the name of the field, not its form value

PHP Code:
       $therules = array(
 
         array(
               'field' => 'first',
               'label' => 'First Name',
               'rules' => 'required|trim|max_length[40]'
           ),
           array(
               'field' => 'last',
               'label' => 'Last Name',
               'rules' => 'required|trim|max_length[40]'
           ),
       );
 
 $this->form_validation->set_rules($therules);

 
       if ($this->form_validation->run() == TRUE) {
 
           return true;
 
       } else {
 
           return false;
 
       


so in the rules array its the form field name, the label to show if the field does not validate, and the actual validation rules
Reply




Theme © iAndrew 2016 - Forum software by © MyBB