[英]How could I select the second month of the year and display the correct day(28 or 29) in symfony doctrine form dateType
我該如何選擇一年中的第二個月,並以symfony格式dateType顯示基於leap年的正確日期28或29?
下圖是我使用表單主題自定義的表單。 它顯示得很好,但是問題是當我選擇第二個月時,仍然允許我選擇大於29的日期。我可以在Symfony中對其進行驗證嗎? 還是有其他方法可以做到這一點?
以學說形式:
$builder
->add('startDate', DateType::class, [
'format' => 'yyyy-MM-dd',
])
->add('endDate', DateType::class, [
'format' => 'yyyy-MM-dd',
]);
在視圖形式主題(樹枝)
{% 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 %}
視野中
<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>
您是否必須完全自己實施表格? 有客戶端解決方案,例如: http : //amsul.ca/pickadate.js 。 只是谷歌“ javascript datepicker”。
之后,您還應該驗證服務器端的日期:您可以使用php的checkdate函數:var_dump(checkdate(12,31,2000)); http://php.net/manual/zh/function.checkdate.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.