Welcome Guest, Not a member yet? Register   Sign In
How to use form_validation with PUT data for use with RESTful API?
#1

[eluser]benners[/eluser]
I'm creating a RESTful API http://net.tutsplus.com/tutorials/php/wo...igniter-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);  
  }

}
}
#2

[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.
#3

[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.
#4

[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
#5

[eluser]benners[/eluser]
Thanks. It seems it isn't as simple as POST is Update and PUT is Create. http://stackoverflow.com/questions/63045...st-in-rest
#6

[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?
#7

[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.




Theme © iAndrew 2016 - Forum software by © MyBB