2

I'm creating the WebSocket based application with Codeigniter. So, data is coming as a JSON string(not a POST Method). I want to use the Codeigniter's built-in form_validation method to validate the data received as JSON.

this is what I tried so far to set the variable so I can access it through $this->input->post('variable_name') but.

Try #1

$_POST['variable_name'] = !isset($data['variable_name']) ? NULL : $data['variable_name'];

Try #2

$variable_name = !isset($data['variable_name']) ? NULL : $data['variable_name'];
$this->form_validation->set_value("variable_name", $variable_name);

But when I use $this->input->post('variable_name') it returns NULL.

7
  • 1
    If I remember currectly, CI Input class extracts all the data from the post and caches it in an internal variable, so when you use $this->input->post you are getting values from that cache. That means, that you need to alter the Input class, or set the data to the $_POST array before CI loads. Commented Nov 11, 2015 at 14:22
  • 1
    If so, its a strange behaviour. There should be a way to set values manually. :( Commented Nov 11, 2015 at 14:25
  • Not really, it makes sense - your POST data should not change from the moment your script starts. Commented Nov 11, 2015 at 14:26
  • so, what do you recommend? Commented Nov 11, 2015 at 14:49
  • is there any other way of validation using Codeigniter? Commented Nov 11, 2015 at 14:52

2 Answers 2

9

Here is how I achieved it. Although, It doesn't seems to be an accurate solution but its simple and there is no work around.

I added only one line before execution of form validations

$_SERVER["REQUEST_METHOD"] = "POST";

and then set the variable like this

$_POST['post_var'] = "value";

Now set the validation rules and perform validation.

that's it :)

Sign up to request clarification or add additional context in comments.

1 Comment

You got the answer, that's my pleasure :)
6

There's a method called set_data() that was introduced specifically to allow validation of non-POST inputs.

http://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post

1 Comment

This should be the chosen answer.

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.