Chris Tickner
Chris Tickner

Reputation: 2017

Symfony2 Forms for API and Web

I have an existing website using a fairly "standard" Symfony full-stack, using the Form component to display and process html forms.

Now, I am creating a REST API to retrieve and manipulate our resources, and I'll use the FOSRestBundle to help with that.

I want to offer nice error messages for the forms, and FOSRestBundle ships a default way to do that. Great.

My question is: should I use the SAME form classes I used in Web? Or should I duplicate all of the forms and start from scratch?

The API will need much of the same validation as Web, but some things like Captcha and CSRF are for sure different.

Thanks for any input on this.

Upvotes: 1

Views: 51

Answers (1)

Michael Sivolobov
Michael Sivolobov

Reputation: 13320

It's a matter of taste.

If you have very different forms then it will be easy to do new form-classes. But if you have almost the same you can use your existing forms.

In one of my projects I had different fields in forms for API and Web but I had one Form class. I just check every time: is it API or Web. And manage fields according to the type of request.

Upvotes: 1

Related Questions