3

I created form with date field using single_text widget. I'm choose this one over choice because I'm using Bootstrap datepicker

Problem I have is that field is always populated with current date instead of being empty.

According to this it should work if i set required to false but id does not. I tried setting empty_value to empty string same for data but in first case nothing happened probably because this option is for choice fields. In second case I'm getting exception "Expected argument of type "\DateTime", "string" given"

I tried using DataTransformer but didn't make any difference. I found out that for data fields value always goes through DateTimeToLocalizedStringTransformer and if I understand it correctly it is returning empty string if empty value so problem is somewhere further after datatransformers.

One more thing I tried is to set value using attr array and it worked unfortunately the side effect was that populating form with some values doesn't affect date field.

Is there any way to set default value of data field as empty with single_text widget?

Here goes a code

<?php

namespace Psw\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;
use Psw\AdminBundle\Form\DataTransformer\EmptyDateTransformer;
use Psw\AdminBundle\Form\DataTransformer\EmptyDateViewTransformer;

class OrdersFilterType extends AbstractType
{
    private $admin;

    public function __construct($admin=false) {
        $this->admin = $admin;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('client', 'entity', array(
                'class' => 'PswAdminBundle:User',
                'required' => false,
                'multiple' => false,
                'label'     => 'orders.client',
                'empty_value' => 'orders.allclients',
                'query_builder' => function(EntityRepository $er) {
                    $qb = $er->createQueryBuilder('u');
                    return $qb->where($qb->expr()->like('u.roles', '?0'))
                        ->setParameters(array('%ROLE_CLIENT%'));
                }
            ));
        if($this->admin) {
            $builder->add('staff', 'entity', array(
                'class' => 'PswAdminBundle:User',
                'required' => false,
                'multiple' => false,
                'label'     => 'orders.staff',
                'empty_value' => 'orders.allStaff',
                'query_builder' => function(EntityRepository $er) {
                    $qb = $er->createQueryBuilder('u');
                    return $qb->where($qb->expr()->like('u.roles', '?0'))
                              ->orWhere($qb->expr()->like('u.roles', '?1'))
                              ->orWhere($qb->expr()->like('u.roles', '?2'))
                        ->setParameters(array('%ROLE_STAFF%','%ROLE_ADMIN%','%ROLE_SUPER_ADMIN%'));
                }
            ));
        }
        $builder->add('start', 'date', array(
                    'label' => 'orders.start',
                    'widget' => 'single_text',
                    'required' => false,
                ))
                ->add('end', 'date', array(
                    'label' => 'orders.end',
                    'widget' => 'single_text',
                    'required' => false,
                ))
                ->add('min', null, array(
                    'label' => 'orders.min',
                    'required' => false,
                )) 
                ->add('max', null, array(
                    'label' => 'orders.max',
                    'required' => false,
                ));
    }


    public function getDefaultOptions(array $options)
    {
        $options = parent::getDefaultOptions($options);
        $options['csrf_protection'] = false;

        return $options;
    }

    public function getName()
    {
        return 'psw_adminbundle_ordersfiltertype';
    }
}
Gustek
  • 3,680
  • 2
  • 22
  • 36
  • Can you provide the code? Form type and entity property (if your field is mapped to it) as well. – ozahorulia Mar 08 '13 at 17:54
  • Code provided ;] I don't map it to any entity. Fields in questoin are just after entitiy fields start/end – Gustek Mar 08 '13 at 18:08
  • The same code works for me, it's weried. Have you tried to set 'format' option? – ozahorulia Mar 08 '13 at 18:36
  • No difference with format. I turned off date picker and nothing change so it is not its fault either. I'm using Symfony 2.1.9. I tried to update it to 2.2 but got errors that I shouldn't do it. – Gustek Mar 08 '13 at 18:47
  • I'm using this version to, but nothing like that happens. I have no idea what's wrong, sorry :( – ozahorulia Mar 08 '13 at 19:04
  • @Gustek have you found a solution to this problem? – Dalen Jan 29 '15 at 09:06

0 Answers0