Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter form validation always false with json data

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? :)

like image 838
ahmedsaber111 Avatar asked Jun 07 '26 11:06

ahmedsaber111


1 Answers

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";        
}
like image 195
abdulwadood Avatar answered Jun 10 '26 04:06

abdulwadood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!