Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Typehinting DateTime in a form cause Exception instead of error message in the form #38324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AllanFousse opened this issue Sep 27, 2020 · 2 comments

Comments

@AllanFousse
Copy link

AllanFousse commented Sep 27, 2020

Symfony version(s) affected: 5.1.6 (same with 5.1.5)

Description
typehinting DateTime in a Form cause bugs...

If you dont put a value in a DateType in a Form, you have a Exception (Typed property App\Form\BugForm::$date must be an instance of DateTime, null used) with php 7.4 typed definition of the date (public \DateTime $date;) in place of a error message in the form.

How to reproduce

class BugForm
{

/**
 * @Assert\NotNull()
 */
public \DateTime $date;

}

class BugType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('date', DateType::class, ['widget' => 'single_text']);
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(
        [
            'attr' => ['novalidate' => 'novalidate'],
            'data_class' => BugForm::class,
        ]
    );
}

}

To have the normal comportment, just dont use php 7.4 typed definition ("public $date" instead of "public \DateTime $date")

@ro0NL
Copy link
Contributor

ro0NL commented Sep 28, 2020

@NotNull implies you expect a null value (to be validated as such), but you don't account for it in your model:

public ?\DateTime $date = null;

would do, isnt it? AFAIK there are many related issues: #36350 #36051 #37377

@xabbuh
Copy link
Member

xabbuh commented Sep 29, 2020

Closing as explained

@xabbuh xabbuh closed this as completed Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants