【问题标题】:Angular and CodeIgniter Validation LibraryAngular 和 CodeIgniter 验证库
【发布时间】:2013-09-26 15:19:12
【问题描述】:

我正在尝试使用 CodeIgniter 对 Angularjs 发布的 json 数据的验证库。问题是当我去设置验证规则时,json 对象未被 Codeigniter 识别为 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()); 
       }

【问题讨论】:

    标签: php json codeigniter validation angularjs


    【解决方案1】:

    如果 Angular 通过 POST 发送数据,它们应该是可用的。试试var_dump($_POST) 看看里面是否有任何东西(可能是格式错误?)。

    如果没有,你可以试试这个“hack”:

     $_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()); 
    }
    

    【讨论】:

    • 打开网站管理员工具(在 chrome 中)或 firebug(在 firefox 中)并查看发送到服务器的内容。
    猜你喜欢
    • 1970-01-01
    • 2010-10-15
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多