0

I have a DateTimeWidget rendered by 2 fields:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('datetime', DateTimeType::class, [
            'date_widget' => 'single_text',
            'date_format' => 'dd/MM/yyyy',
            'time_widget' => 'single_text',
        ]);

It generates as needed 2 field widget:

<input type="text" id="form_datetime_date" name="form[datetime][date]">
<input type="text" id="form_datetime_time" name="form[datetime][time]">

It works perfectly using the UI.

During my functional test, i want to submit the form having this input.

I tried

$form = $crawler->selectButton('my_submit')->form();
$form['form']['datetime'];

and

$form = $crawler->selectButton('my_submit')->form();
$form['form']['datetime']['date'] = new \DateTime('tomorrow');
$form['form']['datetime']['time'] = new \DateTime('tomorrow');

Writing this question I found the solution, I let it here if it can help someone

goto
  • 7,908
  • 10
  • 48
  • 58

1 Answers1

0

It has to be filled as text separately

$form->disableValidation();
$values = $form->getPhpValues();

$form['form']['datetime'] = [
    'date'=> (new \DateTime('tomorrow'))->format('d/m/Y'),
    'time'=> (new \DateTime('tomorrow'))->format('H:i'),
];

$crawler = $client->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles());
goto
  • 7,908
  • 10
  • 48
  • 58