How could I select the second month of the year and display the correct day, 28 or 29 based on leap year, in symfony doctrine form dateType?
The picture below is the form I use form theme to customize. It's display well but the problem is when I select the second month it still let me select the day bigger than 29. Could I validate it in Symfony? Or has other ways to do that?
In doctrine form:
$builder
->add('startDate', DateType::class, [
'format' => 'yyyy-MM-dd',
])
->add('endDate', DateType::class, [
'format' => 'yyyy-MM-dd',
]);
In views form theme(twig)
{% block date_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{- block('form_widget_simple') -}}
{% else %}
{% set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) %}
{% if datetime is not defined or not datetime %}
<div {{ block('widget_container_attributes') }}>
{%- endif %}
{{ date_pattern|replace({
'{{ year }}': form_widget(form.year, {'attr': {'id': 'year', 'name': 'year', 'class': 'form-control'}}),
'{{ month }}': form_widget(form.month, {'attr': {'id': 'month', 'name': 'month', 'class': 'form-control'}}),
'{{ day }}': form_widget(form.day, {'attr': {'id': 'day', 'day': 'day', 'class': 'form-control'}}),
})|raw }}
{% if datetime is not defined or not datetime %}
</div>
{%- endif -%}
{% endif %}
{% endspaceless %}
{% endblock date_widget %}
In view
<div class="col-xs-12" style="margin-top:10px">
<div class="form-inline">
<div class="form-group">
<label>Effective Time: </label>
</div>
<div class="form-group">
{{ form_widget(form.startDate) }}
</div>
<div class="form-group">
<label> - </label>
</div>
<div class="form-group">
{{ form_widget(form.endDate) }}
</div>
</div>
</div>
Do you have to implement the form entirely by yourself? There are client-side solutions like: http://amsul.ca/pickadate.js. Just google "javascript datepicker".
After that, you should validate the date serverside aswell: You could use php's checkdate function: var_dump(checkdate(12, 31, 2000)); http://php.net/manual/en/function.checkdate.php