3

I am using codeigniter to build my own REST web service, The application have a resource called "calls" and can get access to it via POST method,

The "calls" function receive four parameters [id, manager, department, level] as a json object via a Curl call using this content-type:application/json

Then I am using codeigniter form_validation() library to validate the request then return array of the response if no error on the form_validation()

The problem was that the form_validation() always returns FALSE although I can output the received value as expected.

Below are the code of calls function

function calls_post() {

$this->load->model('api/central');
$this->load->library('form_validation');

//validation rules
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('manager', 'manager', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');
$this->form_validation->set_rules('level', 'level', 'required|numeric');


if ($this->form_validation->run() === FALSE) {

    $employeeId = $this->form_validation->error('id') ? $this->form_validation->error('id') : $this->post('id');
    $manager = $this->form_validation->error('manager') ? $this->form_validation->error('manager') : $this->post('manager');
    $department = $this->form_validation->error('department') ? $this->form_validation->error('department') : $this->post('department');
    $level = $this->form_validation->error('level') ? $this->form_validation->error('level') : $this->post('level');

    $response = array(
        'status' => FALSE,
        'error' => 'We don\'t have data to display, Minimum information required to process the request is missing',                
        'authenticated' => true,                
        'id' => $employeeId,
        'manager' => $manager,
        'department' => $department,
        'level' => $level                
    );

    $this->response($response, 200);

} else {

    $response = $this->central->calls_get($this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level'));
    $this->response($response);
}
}

Any advice? :)

1 Answer 1

14

http://ellislab.com/forums/viewthread/238080

$_POST = json_decode(file_get_contents("php://input"), true);
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');

if($this->form_validation->run() == TRUE)
{   
    $id = $this->input->post('id'); 
    $department = html_escape($this->input->post('department'));    
    echo $id."<br>".$department;       
}
else
{   
    echo "false";        
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much buddy.I am trying to perform form validation when we make curl post request on server.
amazing, so by default ci3 form_validator not support json pyload right ?

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.