CodeIgniter Forums
How to use form_validation with PUT data for use with RESTful API? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to use form_validation with PUT data for use with RESTful API? (/showthread.php?tid=53133)



How to use form_validation with PUT data for use with RESTful API? - El Forum - 07-12-2012

[eluser]benners[/eluser]
I'm creating a RESTful API http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/

I'm having problems displaying form_validation errors and I'm thinking the problem is that the data is PUT rather than POST. Can anyone assist?

Code:
To create a new user the API receives a PUT request.

public function create_put() {
if(!$this->put('email')) {  
  $this->response(array('status' => 'missing_data'), 400);  
} else {
  $this->load->library('form_validation');
  $this->form_validation->set_rules('email', 'Email address', 'trim|valid_email');
  if($this->form_validation->run() === FALSE)
  {
   $this->response( array( 'status' => validation_errors() ), 406);  
  }

}
}



How to use form_validation with PUT data for use with RESTful API? - El Forum - 07-13-2012

[eluser]benners[/eluser]
I've realised that I should be using POST to create a record rather than PUT and so the validation now works.

I still don't know how I'd access form_validation when doing an update with PUT so would appreciate any suggestions.


How to use form_validation with PUT data for use with RESTful API? - El Forum - 07-13-2012

[eluser]LuckyFella73[/eluser]
According to the tutorial updates of data are made with POST request.
That way you can use the form_validation as well.


How to use form_validation with PUT data for use with RESTful API? - El Forum - 07-13-2012

[eluser]CroNiX[/eluser]
Someone modded form val to use PUT, but not sure if they will merge it in due to the comments in the pull request by narfbg.
https://github.com/EllisLab/CodeIgniter/pull/1582



How to use form_validation with PUT data for use with RESTful API? - El Forum - 07-13-2012

[eluser]benners[/eluser]
Thanks. It seems it isn't as simple as POST is Update and PUT is Create. http://stackoverflow.com/questions/630453/put-vs-post-in-rest


How to use form_validation with PUT data for use with RESTful API? - El Forum - 09-13-2012

[eluser]Drinian[/eluser]
Couldn't you just write the PUT values into the $_POST array and then use form validation? Is this a bad hack?


How to use form_validation with PUT data for use with RESTful API? - El Forum - 09-13-2012

[eluser]Drinian[/eluser]
Looks like this has been fixed in the REST server:

Phil Sturgeon's REST server on GitHub

EDIT:

This still has to be merged into CodeIgnitor I believe. The fix is actually in Form_validation.php.