我正在尝试向Angularjs发布的json数据上的CodeIgniter验证库。问题是,当我去设置验证规则时,Codeigniter不会将json对象识别为post值。有人知道如何为json对象设置Codeigniter验证规则吗?
$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());
}
答案 0 :(得分:1)
如果Angular通过POST发送数据,它们应该可用。试试var_dump($_POST)
看看那里是否有任何东西(可能是格格不入?)。
如果没有,你可以尝试这个“黑客”:
$_POST = json_decode(file_get_contents("php://input"), true);
验证前。
所以,做一些像:
$_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());
}