rvanlaak/SettingsBundle

Load settings choices from database

Opened this issue · 4 comments

seydu commented

I have a use case where putting the choices for a setting in the configuration files is not naturel.
I have content stored in a database and users can select some items from this content and put it
in their preferences.

The form type class Dmishh\Bundle\SettingsBundle\Form\Type/SettingsType builds choices from the configuration and I think it does not allow to provide choices from another source.

By design the Symfont Form Component does not allow to change form options after it has been built.

Letting dev provide the choices from another source would add more flexibilite and allow building more complex settings.

What about doing it using Symfony Event dispatcher ?

It's a good idea, actually. But how do you imagine utilization of the Event Dispatcher for this task?

seydu commented

I am thinking of creating a tag to handle event listener services. An event will be fired for each field options before building the form field.

In the meantime a have changed the SettingsType class to call call a method that provides the options.
This allows child classes to provide the extra options. See feature-custom-form-options

This is enough for my use case. If there is a more global need for this kind of behaviour I will submit a PR for the event listeners.

Symfony 2.7 improved the ChoiceType, so we could make use of the choice_loader to make the setting choices dynamic.

More info: http://symfony.com/blog/new-in-symfony-2-7-choice-form-type-refactorization

I can envision using this database persistence for user preferences that would exist across sessions. Is there any expectation of a date for a 2.1 release?