javascript - Symfony 4 - Can not customize attr for form field in formBuilder

805

Solution:

You can also pass parameters to form fields when you render the form

{{ form_row(form.activeBorderRadius, {'attr': {'class': 'custom-control-input', 'id': 'toggle-border-radius'}}) }}
{{ form_label(form.activeBorderRadius, {'attr': {'class': 'custom-control-label', 'for': 'toggle-border-radius'}}) }}
401

Answer

Solution:

You can use a "class" rather than an ID.

$(".custom-control-input").change(e => {
    e.preventDefault();
    $('.page-wrapper').toggleClass("boder-radius-on");
});

If you still want to use ID, you can create your own type in which you specify your code:

{% block you_widget %}
  {% set _class = '' %}
  {% if attr.class is defined %}
    {% set _class = ' ' %}
  {% endif %}
  {% set _label_class = '' %}
  {% if label_attr.class is defined %}
    {% set _label_class = ' ' %}
  {% endif %}
  <input type="checkbox" id="{{ id }}" name="{{ full_name }}"{{ _class }}>
  <label for="{{ id }}"{{ _label_class }}></label>
  <script>
    $('{{ id }}').change(e => {
      e.preventDefault();
      $('.page-wrapper').toggleClass('border-radius-on');
    });
  </script>
{% endblock you_widget %}

How to create your form type see here.

People are also looking for solutions to the problem: php - Pandoc causes problem when knit .Rmd file (Raspbian)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.