Appending an option to a select box inside twig template using Symfony 5

I am rendering a form inside a twig file. I just want to add a default/selected option to the select field and I need to do it inside the twig rather than controller, entity, or the formType.

Here is the code in my twig file:

{{ form_start(prdForm) }}
    <div class="form-group">
       {{ form_row(prdForm.productTitle, {'label': "Product Title", 'attr': {'placeholder': "Enter Product Name", 'class': "form-control"}}) }}
    <div class="form-group">
       {{ form_row(prdForm.productDescription, {'label': "Description", 'attr': {'placeholder': "Provide a short description for this product.", 'class': "form-control"}}) }}
    <div class="form-group">
        <option value="" selected="selected" disabled="disabled">Choose an item...</option>
        {{ form_widget(prdForm.category, {'label': "Select Category", 'attr': {'class': "form-control"} }) }}
{{ form_end(prdForm) }}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *