Symfony & Twig: Simple month and year form selection widget

This is a very useful Twig that I use in a lot of different forms.  I attach a listener and when they click “Update” it will redirect the user by appending the URL with the /year/month to hook nice and cleanly in built-in routes to pass the year and month into different controllers.

<form id="yearMonthWidget" name="newDate1" method="get" action="">

   <select name="month">
      {% for mo in 1..12 %}
         <option value="{{ mo }}">{{ ('2012-' ~mo~'-01')|date("M") }}</option>
      {% endfor %}

   </select>

   <select name="year">
      {% for yr in "now"|date("Y")+1..2010 %}
         <option value="{{ yr }}">{{ yr }}</option>
      {% endfor %}
   </select>
   <input type="submit" value="Update"/>
</form>

 

About Author:

Senior Cloud Software Engineer and 25+ years experienced video production, video editing and 3D animation services for a variety of global clients including local video production here in Jacksonville, Florida.

Leave a Comment

Your email address will not be published. Required fields are marked *