【问题标题】:Validation of an API in codeigniter not working properly在 codeigniter 中验证 API 无法正常工作
【发布时间】:2016-11-07 09:15:21
【问题描述】:

我正在使用 codeigniter 编写 API。我正在使用 codeigniter 内置函数验证它具有的字段,但不知何故它没有按应有的方式工作。

public function check_validation()
{

     $this->load->helper(array('form', 'url'));
     $this->load->library('form_validation');


     $firstname     = mysql_real_escape_string($this->input->post('firstname'));
    $lastname   = mysql_real_escape_string($this->input->post('lastname'));
    $email  = mysql_real_escape_string($this->input->post('email'));*/


     //$firstname = 'Numaan';
     //$lastname = 'sheikh';
     //$email = 'test@test.com';


    $this->form_validation->set_rules('firstname', 'Username', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required');


    if ($this->form_validation->run() == FALSE)
        {
                $finalResult = array('code' => 100,
                'msg'=>'Field Emsdsdspty.',
                'data' => array()
                );
        }
        else
        {
                $finalResult = array('code' => 100,
                'msg'=>'Validation Successful.',
                'data' => array()
                );
        }

    echo json_encode($finalResult);

}

我正在尝试获取发布的值,但它无法正常工作。然后我还尝试将值分配给变量,然后通过验证它也没有工作。

【问题讨论】:

    标签: php codeigniter validation


    【解决方案1】:

    试试这个(我不测试)

    function check_validation()
    {
    
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
    
        //default result
        $finalResult = array('code' => 500, 'msg' => 'Unknow Error', 'data' => array());
    
        if($this->input->post()) {
            //set validaton
            $this->form_validation->set_rules('firstname', 'Username', 'trim|strip_tags|required');
            $this->form_validation->set_rules('lastname', 'Lastname', 'trim|strip_tags|required');
            $this->form_validation->set_rules('email', 'Email', 'trim|strip_tags|required');
    
            //check form
            if($this->form_validation->run() == TRUE) {
                $finalResult = array('code' => 200, 'msg' => 'Success', 'data' => array()); 
            }else {
                $finalResult = array('code' => 400, 'msg' => validation_errors(), 'data' => array());
            }
        }
    
        echo json_encode($finalResult);
    }
    

    ps。 api状态码:http://www.restapitutorial.com/httpstatuscodes.html

    【讨论】:

      猜你喜欢
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多