2

I am fighting with customize on symfony forms for twig...

My problem is that I want a specific look and feel for a field. Here is the code :

class FormType extends AbstractType
{

    //Builds the form
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('url', UrlType::class, array(
                'label' => false,
                'required' => true,
                ))
        ;
    }
}



{# Twig template #}

{% form_theme form _self %}

{{ form_start(form) }}
    {{ form_widget(form) }}

    {% block url_widget %}
    <div class="col-md-12">
        <span class="input-group-addon">url</span>
        <input type="url" id="url" name="url" class="form-control input-lg" />
    </div>
    {% endblock url_widget %}

    <div class="form-group text-center">
        <button type="submit" name="submit" title="send">
            Send
        </button>
    </div>
{{ form_end(form) }}

But when I use this I have two fields in place of one.

[EDIT : split in 3 questions due to on hold status]

[EDIT 2 : solution found] As the question is still on hold (and in case it's displayed one day), I put the solution I've found, below.

As my field is named url it conflicts with url_widget (I think) so I have overrided both the standard block url_widget and the full name url_url_widget. The code is the following:

{% form_theme form _self %}

{% block content %}
    {{ form_start(form) }}
        {{ form_widget(form) }}

        {% block url_widget %}
        {% endblock url_widget %}

        {% block url_url_widget %}
            <div class="col-md-12">
                <span class="input-group-addon">url</span>
                <input type="url" id="url" name="url" class="form-control input-lg" />
            </div>
        {% endblock url_url_widget %}

        <div class="form-group text-center">
            <button type="submit" name="submit" title="send">
                Send
            </button>
        </div>
    {{ form_end(form) }}

{% endblock %}

Another solution is to rename my field in my FormType, what I will probably do...

Laurent
  • 289
  • 1
  • 3
  • 17
  • Please split your question in several questions. It's recommended to have only one problem per question. – A.L Apr 22 '16 at 22:54
  • Yes I was thinking doing this first, but as the main problem is the same, cutomizing form, I did it in one question to group – Laurent Apr 23 '16 at 08:19
  • Now that is has been put on hold... Thing I didn't know about it, I will split in 3 questions – Laurent Apr 23 '16 at 15:50
  • As I don't know how works the "on hold", will my question be unset "on hold" automatically, or do I have something to do ? Thanks – Laurent Apr 25 '16 at 08:49
  • You should have a link to **reopen** your question. Then there will be a vote in order to decide if the question will be reopened – A.L Apr 25 '16 at 08:55
  • Thanks. I can't see/find that link... :-( – Laurent Apr 25 '16 at 12:05
  • I'm sorry, I was wrong, in fact it's automatic: *Closed questions that receive edits within the first 5 days of closure are automatically put into a review queue to be considered for reopening* Source: http://stackoverflow.com/help/reopen-questions – A.L Apr 25 '16 at 12:13
  • Ok thanks! So let's wait – Laurent Apr 25 '16 at 12:23

1 Answers1

1
  1. Move out block "url_widget" out of content block
    http://symfony.com/doc/current/cookbook/form/form_customization.html#method-1-inside-the-same-template-as-the-form

  2. If you don't want escaping labels also use form themes
    HTML in Symfony2 form labels instead of plain text

  3. Options are translated automatically
    Translate select options in Symfony2 class forms

Community
  • 1
  • 1
luchaninov
  • 6,792
  • 6
  • 60
  • 75
  • Thanks for yours answers. For 1) I have tried outside of the block content or with `{{ form_ro(form.url) }}` but I still have 2 inputs. For 2) I have tried with the raw (even at diferent places), but the html are still escaped (I have added the code I use above). For 3) yes, but the problem is that my label contains numbers and results of operations with text, so the content is not only a full translation placeholder, i.e. 1 year -5 credits, where year and credits have to be translated. But really thanks for your help! – Laurent Apr 23 '16 at 08:17