craue / CraueConfigBundle

Database-stored settings made available via a service for your Symfony project.
MIT License
173 stars 35 forks source link

Field Type #29

Closed shairyar closed 8 years ago

shairyar commented 8 years ago

Hi,

Great bundle and I am so glad to find it. I was wondering if it is possible to set the field type, for example if i want an option to display as a checkbox or dropdown menu how can i achieve it? I know the view to render the form is modify_form.html.twig but the fields that are being rendered are all TextType

craue commented 8 years ago

In short: the field type is fixed.

I'd like to keep the bundle as simple as it is. If your app has more complex requirements, you'll probably need a custom solution. See #5 for the discussion.

shairyar commented 8 years ago

What if we dont use the following way to render the form

{% for setting in form.settings if setting.section.vars.value == section %}
            {{ form_row(setting.name) }}
            {{ form_row(setting.section) }}
            {{ form_row(setting.value, {
                    'label': setting.name.vars.value | trans({}, 'CraueConfigBundle'),
                }) }}
{% endfor %}

and use the following way to render the form with simple html

<input type="hidden" id="craue_config_modifySettings_settings_1_name"
                           name="craue_config_modifySettings[settings][1][name]" value="testing">
                    <input type="hidden" id="craue_config_modifySettings_settings_1_section"
                           name="craue_config_modifySettings[settings][1][section]" value="dummy">
                    <input type="text" id="craue_config_modifySettings_settings_1_value"
                           name="craue_config_modifySettings[settings][1][value]" class="form-control"
                           value="{{ form.settings.vars.data[1].value }}">

I am able to render a checkbox this way and get it to work :)

This way ofcourse we wont be using the loop but we will be adding the options manually to render them, i wonder if this way has any cons? it seems to work fine without any problem. This way i am creating the sections manually and putting each new field manually in respective section, if it is text or checkbox, yet to test it with dropdown though.

craue commented 8 years ago

Feel free to override the template. :smirk:

shairyar commented 8 years ago

This leads to one problem, each time you create a new field the order of the array gets changed so suppose you made the index 2 a checkbox, it will not longer be a checkbox as soon as you add a new field as the index as been replaced. One way of fixing this is by adding an id auto incremented field in entity and always pull entity byorder id this way the index will remain the same. Does symfony allow to override an entity or i will have to make this change inside the entity of your bundle?

shairyar commented 8 years ago

Okay so i made the change inside your entity by adding auto increment id after doing this i had to drop the schema and recreate it. Now i have checkbox, dropdown and text field on the form all are saving data and displaying the data correctly.

I still have a question though, now that i no longer am using the symfony form widget in twig, i hope this will not create any problem down the line, so far it seems to work perfectly.

Many thanks for the wonderful bundle.