symfony - Disable birthdate choice field -
i have form field entering birthdate.
$builder->add('birthdate', 'birthday', array('label' => 'birthdate', 'translation_domain' => 'messages', 'required' => false, 'widget' => 'choice'))
in cases want disable birhtdate modifications in twig template, like.
{{ form_row(form.birthdate.day, {'attr': {'disabled': true}}) }} {{ form_row(form.birthdate.month, {'attr': {'disabled': true}}) }} {{ form_row(form.birthdate.year, {'attr': {'disabled': true}}) }}
this works fine, if render complete row in 1 statement like:
{{ form_row(form.birthdate, {'attr': {'disabled': true}}) }}
this don't disable birthdate field. have same issue?
i solve issue, problem attributes not injected date_widget
i ovewrite date_widget
, fine.
before modify it:
{% block date_widget -%} {% if widget == 'single_text' %} {{- block('form_widget_simple') -}} {% else -%} {% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%} {% if datetime not defined or not datetime -%} <div {{ block('widget_container_attributes') -}}> {%- endif %} {{- date_pattern|replace({ '{{ year }}': form_widget(form.year), '{{ month }}': form_widget(form.month), '{{ day }}': form_widget(form.day), })|raw -}} {% if datetime not defined or not datetime -%} </div> {%- endif -%} {% endif %} {%- endblock date_widget %}
after modification
{% block date_widget -%} {% if widget == 'single_text' %} {{- block('form_widget_simple') -}} {% else -%} {% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%} {% if datetime not defined or not datetime -%} <div {{ block('widget_container_attributes') -}}> {%- endif %} {{- date_pattern|replace({ '{{ year }}': form_widget(form.year, {'attr': attr}), '{{ month }}': form_widget(form.month, {'attr': attr}), '{{ day }}': form_widget(form.day, {'attr': attr}), })|raw -}} {% if datetime not defined or not datetime -%} </div> {%- endif -%} {% endif %} {%- endblock date_widget %}
you should not disable in template, when build form. (see birthday form reference)
Comments
Post a Comment