Ask your Symfony questions! Pay money and get answers fast! (more info)

Dynamic form generation (sfWidgets, I18N, validation) Symfony

My application has three levels (frontend, backend and super backend):
- Frontend is accessible to anybody,
- Backend is accessible to my customers,
- Super backend is the same app as backend but with more options, available for super users only.

The backend gives the clients the possibility to build forms (form builder). It currently uses several tables:

Concept (name of the form)
Components (Component definition, validation, component name, type, linked to the form) - It doesn't use sfWidgets.
Component_translations (Translation for label, validations errors, ect...)
Component_choice (Which choices are linked to which component)
Choice, Choice translation (a choice is an option in a combo list or radio button list)
...

The forms need to be both client side and server side validated. It currently doesn't use the form framework at all.

The generated forms are then visible on the frontend, they should be 100% I18N (if the client defined translations). What's more, when the form is saved, the result displayed on the frontend should be 100% 18N too.

It goes without saying that the overall setup/configuration is quite heavy. It works as expected but I loose the power of Symfony form framework (no CSRF protection, no validators, ...)

I'm looking for suggestions, plugins, best practices that would address my needs.

Symofony 1.4, soon to be migrated to 2.0.


Edit (4th of Sept):
Basically I'm working on a "wysiwyg form builder", multiple forms can be defined. Each form has multiple possible components (text, text area, select, radios, image upload, ...). Currently, I'm not leveraging Symfony form framework power (nor to build, nor to validate, nor to save it), all is done "in-house". I'm looking for recommendations, ideas how to store/validate/save the forms using symphony framework.

Answers (1)

2011-09-04

Ben answers:

I don't think you have provided enough information here to give you much help - could you post a schema, and more of an explanation of the purpose of the project?

Edit 4 Sept:
Are you using Doctrine?

The Symfony form framework is extremely powerful, and you can dynamically build forms using it by adding custom methods.

If the reason you have decided not to use it is that you are worried it is not customisable enough, I think if you spend some time with it you will realise that it is actually extremely flexible, and will provide you with a huge amount of functionality that will take ages to implement yourself.

i18n is amazingly well integrated into the symfony form framework.

By adding a custom implementation of BaseFormDoctrine you can add methods that will be inherited by all child forms.

based on your description it sounds like you could use the Builder design pattern
http://en.wikipedia.org/wiki/Builder_pattern

If you use symfony's form framework you can have a builder class that will take your form definition (how you define each form I guess will depend on your implementation, but I imagine you'll have a meta table containing each form field and its type) and dynamically add the appropriate widgets and validators. Then you just need to implement a custom save method on the form to handle your logic.

There are loads of custom widgets that have been made for symfony's form framework that are available as plugins

Frontend validation: http://www.symfony-project.org/plugins/sfJqueryFormValidationPlugin
Image cropping: http://www.symfony-project.org/plugins/sfDoctrineJCroppablePlugin
Google maps: http://www.symfony-project.org/plugins/sfEasyGMapPlugin

Hope this is helpful anyway.


Ben comments:

Are you using Doctrine?

The Symfony form framework is extremely powerful, and you can dynamically build forms using it by adding custom methods.

If the reason you have decided not to use it is that you are worried it is not customisable enough, I think if you spend some time with it you will realise that it is actually extremely flexible, and will provide you with a huge amount of functionality that will take ages to implement yourself.

i18n is amazingly well integrated into the symfony form framework.

By adding a custom implementation of BaseFormDoctrine you can add methods that will be inherited by all child forms.

based on your description it sounds like you could use the Builder design pattern
http://en.wikipedia.org/wiki/Builder_pattern

If you use symfony's form framework you can have a builder class that will take your form definition (how you define each form I guess will depend on your implementation, but I imagine you'll have a meta table containing each form field and its type) and dynamically add the appropriate widgets and validators.

There are loads of custom widgets that have been made for symfony's form framework that are available as plugins

Frontend validation: http://www.symfony-project.org/plugins/sfJqueryFormValidationPlugin
Image cropping: http://www.symfony-project.org/plugins/sfDoctrineJCroppablePlugin
Google maps: http://www.symfony-project.org/plugins/sfEasyGMapPlugin

Hope this is helpful anyway.