Ishaque Javed
Ishaque Javed

Reputation: 313

How To Set Date Range In Symfony DateType Field?

I Need To set Range on Minimum and maximum date on dateType field in Form. My Code is here.

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('date',DateType::Class, array(
        'widget' => 'single_text',
        'attr' => array(
            'min' => new \DateTime(),
            'max' => new \DateTime('+7 day'),
    )));
}

Upvotes: 3

Views: 18569

Answers (2)

Se Developers
Se Developers

Reputation: 168

 public function buildForm(FormBuilderInterface $builder, array $options){
       $builder->add('date',DateType::Class, array(
                 'widget' => 'choice',
                 'years' => range(date('Y'), date('Y')+100),
                 'months' => range(date('m'), 12),
                 'days' => range(date('d'), 31),
               ));
 }

Upvotes: 10

E.K.
E.K.

Reputation: 1055

Probably you'll get an error because you've set min and max attributes as DateTime object instead of text. Convert they to string (use format() method). Something like this:

'min' => (new \DateTime())->format('c'), //use format you need

Upvotes: 1

Related Questions