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

Skip to content

[Form] Add input_format option to DateType and DateTimeType #29887

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CHANGELOG
}
}
```
* added new option `input_format` to `DateType` and `DateTimeType` to specify the date format when using the `string` input.

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'])
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
Expand Down Expand Up @@ -251,6 +251,7 @@ public function configureOptions(OptionsResolver $resolver)
'empty_data' => function (Options $options) {
return $options['compound'] ? [] : '';
},
'input_format' => 'Y-m-d H:i:s',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we set the default here or rely on the transformer default ? IMHO, it's better here so the form type and the transformer are more decoupled.

]);

// Don't add some defaults in order to preserve the defaults
Expand Down Expand Up @@ -317,6 +318,8 @@ public function configureOptions(OptionsResolver $resolver)

return '';
});

$resolver->setAllowedTypes('input_format', 'string');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we validate the passed input_format ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return sprintf('Using the "date_format" option of %s when the "widget" option is set to "single_text" is deprecated since Symfony 4.3 and will lead to an exception in 5.0.', self::class);
//throw new LogicException(sprintf('Cannot use the "date_format" option of the %s when the "widget" option is set to "single_text".', self::class));

As we are going to do with the time_widget option, I wonder if we should throw an exception when the input_format is passed and the value of input is not strictly equal to string'.

}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class_exists('IntlTimeZone', false) ? \IntlTimeZone::createDefault() : null,
$builder->addModelTransformer(new DateTimeImmutableToDateTimeTransformer());
} elseif ('string' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], 'Y-m-d')
new DateTimeToStringTransformer($options['model_timezone'], $options['model_timezone'], $options['input_format'])
));
} elseif ('timestamp' === $options['input']) {
$builder->addModelTransformer(new ReversedTransformer(
Expand Down Expand Up @@ -283,6 +283,7 @@ public function configureOptions(OptionsResolver $resolver)
return $options['compound'] ? [] : '';
},
'choice_translation_domain' => false,
'input_format' => 'Y-m-d',
]);

$resolver->setNormalizer('placeholder', $placeholderNormalizer);
Expand All @@ -305,6 +306,8 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('years', 'array');
$resolver->setAllowedTypes('months', 'array');
$resolver->setAllowedTypes('days', 'array');

$resolver->setAllowedTypes('input_format', 'string');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,19 @@ public function provideEmptyData()
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
];
}

public function testSubmitStringWithCustomInputFormat(): void
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'input' => 'string',
'widget' => 'single_text',
'input_format' => 'd/m/Y H:i:s P',
]);

$form->submit('2018-01-14T21:29:00');

$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1037,4 +1037,19 @@ public function provideEmptyData()
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
];
}

public function testSubmitStringWithCustomInputFormat(): void
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
'input' => 'string',
'input_format' => 'd/m/Y',
]);

$form->submit('2018-01-14');

$this->assertSame('14/01/2018', $form->getData());
}
}