Symfony 2: How to make a date field without a day?

I am trying to create a form with a date field where the user can select only the month and year (without the day of the month), but I cannot figure out how to do this.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
        'month',
        'date',
        array(
            'label' => 'Month & year',
            'input' => 'array',
            'widget' => 'choice',
        ));

    $builder->add('submit',
                  'submit');
}

The ideal result is two drop-down lists: a list of months (numerical representation) and a list of years (4 digits, last 5 years).

I think I can use 2 fields of choice type, but maybe there is a more elegant solution?

+4
source share
4 answers

You can do something like this in your twig file.

    {{ form_widget(yourForm.month.month) }}
    {{ form_widget(yourForm.month.year) }}

This will display two selection fields: by month and by year. I suggest changing the name of the field from a month to another. I am not sure, but this may conflict. Hope this helps ...!

: 5 ,

   $builder->add(
     'month',
     'date',
     array(
        'years' => range(date('Y'), date('Y')-5)
        'label' => 'Month & year',
        'input' => 'array',
        'widget' => 'choice',
     ));
+7

, -, , .

Bootstrap Datetimepicker (http://www.malot.fr/bootstrap-datetimepicker/) , :

datetimepicker

script, "mm-yyyy" - .

<script>
        $(function (){
            $(".datepicker").datetimepicker({
                weekStart: 1,
                format : 'mm-yyyy'
            });
        });
</script>

, .

+1

"". , , javascript datepicker, . - ( ) ( Symfony 2.8 ):

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

/**
 * Month Type.
 */
class MonthType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->addModelTransformer(new CallbackTransformer(
                function ($dateTime) {
                    return empty($dateTime) ? null : $dateTime->format('Y-m');
                },
                function ($monthString) {
                    return new \DateTime("$monthString-01");
                }
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['type'] = 'month';
    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return TextType::class;
    }
}
0
source

Perhaps the easiest way to achieve this, if you don't mind sending the day, just hide the day input using the class:

{{ form_start(form) }}
{{ form_widget(form.date.day, {'attr': {'class': 'hide'}}) }}
{{ form_widget(form) }}
{{ form_end(form) }}
0
source

All Articles