2
votes

Hey I'm submiting an array on post where each value is a json string, like this :

Array
(

    [destinations] => Array
        (
            [0] => {"selected_value":185,"destinations":38709,"type":"cl","name":"name1"}
            [1] => {"selected_value":395,"destinations":28867,"type":"cl","name":"name2"}
        )

)

Now when I decode it :

    foreach($destinations as $json){
            $row = json_decode($json,true);
   ...
  }

It works fine, however if I try and run form validation on the destinations :

$this->form_validation->set_rules('destinations', 'Destinations', 'required');

I can no longer json_decode the value, tough it seems to be fine, if I echo it I can see :

{"selected_value":395,"destinations":28867,"type":"cl","name":"name2"}

But the decode doesn't work and $row is null.

Running json_last_error gives : 4 and var_dump of $json gives :

string(143)
 "{"selected_value":185,"destinations":38709,"type":"cl","name":"07h00 תקינים"}" 

Note: the name values are in hebrew, I gave an example in english so easier to read.

I've tried working around it by creating a custom callback function because I thought the implemntation of required is the problem, but I got same results. Does anyone know this bug or/and how can it be fixed? I know I can just check it regullary but I would rather use CI's form validation. I'm using codeigniter 2.1.3, without an option to upgrade.

2

2 Answers

0
votes

Try executing the json_last_error function http://www.php.net/manual/en/function.json-last-error.php. It may help you what in discovering what the error is. Also try doing a var_dump on the variable and post it here.

0
votes

The problem is that form validation is applied only on post data which is not found. You can do the alternative like this

$row = json_decode($json,true);

$_POST['destinations'] = $row['destinations'];

And now

$this->form_validation->set_rules('destinations', 'Destinations', 'required');

This should work.