0

I'm trying to us CodeIgniter's validation library on json data posted from Angularjs. The problem is when I go to set the validation rule the json object is not recognized as a post value by Codeigniter. Anybody know how to set a Codeigniter validation rule for a json object?

    $data = json_decode(file_get_contents("php://input"));
    //die($data->name); = "Bob"
    $this->form_validation->set_rules($data->name, 'Name', 'trim|required|min_length[3]');
       if($this->form_validation->run() == FALSE) { 
          $this->output->set_output(validation_errors()); 
       }

1 Answer 1

1

If Angular sends the data by POST, they should be available. Try var_dump($_POST) to see if there's anything in there (maybe it's malformed?).

If not, you can try this "hack":

 $_POST = json_decode(file_get_contents("php://input"), true);

before your validation.

So, do something like:

$_POST = json_decode(file_get_contents("php://input"), true);
$this->form_validation->set_rules($data->name, 'Name', 'trim|required|min_length[3]');
if(!$this->form_validation->run()) { 
   $this->output->set_output(validation_errors()); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Open a webmasters tool (in chrome) or firebug (in firefox) and take a look at what is being send over to the sever.

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.