json validation in codeigniter
How to validate json data or json nested array in codeigniter
For example we have two fields email and password to validate and sending request from postman as shown below:
request method: post
{
"email":"sachinsharma.one@gmail.com",
"password":"12345"
}
In controller
public function login_post()
{
header('Content-type: application/json');
$request = json_decode(file_get_contents('php://input'),true);
$this->form_validation->set_data($request); /// for setting data
$this->form_validation->set_rules('email','Email', 'required|valid_email');
$this->form_validation->set_rules('password','PASSWORD', 'required|min_length[4]|max_length[20]');
if($this->form_validation->run()==false)
{
echo validation_errors();
}
else
{
// your code..............
}
}
In this way you can validate json data or even big json data