Red
Red

Reputation: 6378

codeigniter form validation error

In my controller i am setting rules like this

$this->form_validation->set_rules("contact_name[]","Contact Name","required|max_length[25]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_title[]","Contact Title","required|max_length[25]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_main[]","Contact Main Number","required|max_length[45]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_direct[]","Contact Direct Number","required|max_length[45]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_cell[]","Contact Cell Number","required|max_length[45]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_primary","Primary","required|max_length[45]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_fax[]","Fax","required|max_length[105]|xss_clean|htmlspecialchars");
        $this->form_validation->set_rules("contact_email[]","Contact Email ID","required|max_length[105]|xss_clean|htmlspecialchars");

and the html is = >

 <input type="text" name="contact_name[]" value="Dileep" class="t_contact_name">
     //and some others too as name as array

after posting i will get something like this

array
  'company' => string 'Sccc' (length=4)
  'name' => string 'Ajay ccc' (length=8)
  'address' => string 'ccsd' (length=4)
  'city' => string 'City name' (length=9)
  'state' => string '20' (length=2)
  'zip' => string '665996555' (length=9)
  'country' => string '6' (length=1)
  'category' => string 'popular' (length=7)
  'credit' => string '2566' (length=4)
  'website' => string 'ccc' (length=3)
  'primary_flag' => string '1' (length=1)
  'discount' => string '0' (length=1)
  'payment_terms' => string '6' (length=1)
  'customerid' => string 'ccc' (length=3)
  'notes' => string 'cc' (length=2)
  'contact_name' => 
    array
      0 => string 'Dileep' (length=6)
      1 => string 'Dileep' (length=6)
      2 => string 'Dileep' (length=6)
      3 => string 'Dileep' (length=6)
      4 => string 'Dileep' (length=6)
      5 => string 'Dileep' (length=6)
      6 => string 'Dileep' (length=6)
      7 => string 'Dileep' (length=6)
      8 => string 'Dileep' (length=6)
  'contact_primary' => 
    array
      0 => string 'true' (length=4)
      1 => string 'false' (length=5)
      2 => string 'false' (length=5)
      3 => string 'false' (length=5)
      4 => string 'false' (length=5)
      5 => string 'false' (length=5)
      6 => string 'false' (length=5)
      7 => string 'false' (length=5)
      8 => string 'false' (length=5)
  'contact_title' => 
    array
      0 => string 'Curio' (length=5)
      1 => string 'Curio' (length=5)
      2 => string 'Curio' (length=5)
      3 => string 'Curio' (length=5)
      4 => string 'Curio' (length=5)
      5 => string 'Curio' (length=5)
      6 => string 'Curio' (length=5)
      7 => string 'Curio' (length=5)
      8 => string 'Curio' (length=5)
  'contact_main' => 
    array
      0 => string '66558-66554-6665' (length=16)
      1 => string '66558-66554-6665' (length=16)
      2 => string '66558-66554-6665' (length=16)
      3 => string '66558-66554-6665' (length=16)
      4 => string '66558-66554-6665' (length=16)
      5 => string '66558-66554-6665' (length=16)
      6 => string '66558-66554-6665' (length=16)
      7 => string '66558-66554-6665' (length=16)
      8 => string '66558-66554-6665' (length=16)
  'contact_direct' => 
    array
      0 => string '5555-666-555' (length=12)
      1 => string '5555-666-555' (length=12)
      2 => string '5555-666-555' (length=12)
      3 => string '5555-666-555' (length=12)
      4 => string '5555-666-555' (length=12)
      5 => string '5555-666-555' (length=12)
      6 => string '5555-666-555' (length=12)
      7 => string '5555-666-555' (length=12)
      8 => string '5555-666-555' (length=12)
  'contact_cell' => 
    array
      0 => string '555-6654-555' (length=12)
      1 => string '555-6654-555' (length=12)
      2 => string '555-6654-555' (length=12)
      3 => string '555-6654-555' (length=12)
      4 => string '555-6654-555' (length=12)
      5 => string '555-6654-555' (length=12)
      6 => string '555-6654-555' (length=12)
      7 => string '555-6654-555' (length=12)
      8 => string '555-6654-555' (length=12)
  'contact_fax' => 
    array
      0 => string '996-5565-656' (length=12)
      1 => string '996-5565-656' (length=12)
      2 => string '996-5565-656' (length=12)
      3 => string '996-5565-656' (length=12)
      4 => string '996-5565-656' (length=12)
      5 => string '996-5565-656' (length=12)
      6 => string '996-5565-656' (length=12)
      7 => string '996-5565-656' (length=12)
      8 => string '996-5565-656' (length=12)
  'contact_email' => 
    array
      0 => string '[email protected]' (length=20)
      1 => string '[email protected]' (length=20)
      2 => string '[email protected]' (length=20)
      3 => string '[email protected]' (length=20)
      4 => string '[email protected]' (length=20)
      5 => string '[email protected]' (length=20)
      6 => string '[email protected]' (length=20)
      7 => string '[email protected]' (length=20)
      8 => string '[email protected]' (length=20)
  'userfile2' => string '' (length=0)
  'contact_log' => 
    array
      0 => string 'Waiting for the release....' (length=27)
  'contact_time' => 
    array
      0 => string '6/30/12' (length=7)
  'contact_duedate' => 
    array
      0 => string '12/01/2012' (length=10)

But when i do run the form validation i am getting some errors

A PHP Error was encountered

Severity: Warning

Message: mb_strlen() expects parameter 1 to be string, array given

Filename: libraries/Form_validation.php

Line Number: 1003
A PHP Error was encountered

Severity: Warning

Message: htmlspecialchars() expects parameter 1 to be string, array given

Filename: libraries/Form_validation.php

Line Number: 619

What is the real problem or what am I doing wrong?

Edit

Please check that the input names are array,there are more than same type values.So i need to use field names as array.

as per the codeigniter Form validation i can validate the form with containng field names as array.

Upvotes: 2

Views: 1700

Answers (1)

Yan Berk
Yan Berk

Reputation: 14428

Your input is consisted of arrays, while the functions max_length and htmlspecialchars expect a single string.

When there is only one value use this type of input:

<input type="text" name="contact_name" value="Dileep" class="t_contact_name">

On inputs where array is a must don't use the aforementioned functions.

Upvotes: 2

Related Questions