Wednesday, March 12, 2014

Re: RESTful (Form) Validation?

I recommend taking a look at https://github.com/friendsofcake/crud, if not for the code (but why not =) ) for an example of how to do what you ask e.g. http://friendsofcake.com/crud/examples/blog.html#validation_errors

curl -I -X POST http://your-site-domain.com/posts/add.json \  	-d title="My new JSON API blog post"    HTTP/1.1 400 Bad Request  Server: nginx/1.4.1  Date: Sun, 16 Jun 2013 12:25:12 GMT  Content-Type: application/json; charset=UTF-8  Content-Length: 69  Connection: keep-alive  Set-Cookie: CAKEPHP=vevclsl2o6r7h4v7uon9j5tkd1; expires=Sun, 16-Jun-2013 16:25:12 GMT; path=/; HttpOnly    {    "success": false,    "data": {      "body": ["This field cannot be left blank"]    }  }

AD

On Wednesday, 12 March 2014 05:06:00 UTC+1, OJ Tibi - @ojtibi wrote:
Hi folks,

I was wondering if there was a way to let the client app know which fields it submitted have validation errors. (As you can see, I placed "Form" between parentheses because there isn't really an HTML form in a REST API request.) While the Cookbook recommends to use the built-in Exception classes to respond to errors in REST requests, there is no recommended way to include Model::validationErrors in the response.

Right now, I'm inclined to create my own subclass of HttpException, but after reading the core files, it looks like I might also need to create a custom ErrorHandler and ExceptionRenderer class, if I understand correctly. Essentially, I just want to add a 'validation_errors' key in the '_serialize' array exported by ExceptionRenderer.

The question is, will I be overdoing it if I did what I outlined above, *or* should I just pass a implode()'d Model::validationErrors as a $message to BadRequestException/InternalErrorException? Also note that Model::validationErrors is a multi-level array with named keys.

Cheers,
OJ

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: