alexvasilyev/datetimepicker-bundle
Installation:
composer require alexvasilyev/datetimepicker-bundle
Enable the bundle in config/bundles.php (Symfony 4+):
SC\DatetimepickerBundle\SCDatetimepickerBundle::class => ['all' => true],
Run asset installation:
php bin/console assets:install public/
Basic Usage:
Add the DatetimeType to your form builder:
use SC\DatetimepickerBundle\Form\Type\DatetimeType;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('eventDate', DatetimeType::class);
}
Twig Integration: Include the form assets in your template:
{% block stylesheets %}
{{ form_stylesheet(form) }}
{% endblock %}
{% block javascripts %}
{{ form_javascript(form) }}
{% endblock %}
DatetimeType as a drop-in replacement for Symfony’s DateTimeType or DateType.pickerOptions to configure the datetime picker:
$builder->add('customDate', DatetimeType::class, [
'pickerOptions' => [
'format' => 'YYYY-MM-DD HH:mm',
'useCurrent' => true,
],
]);
form_stylesheet() and form_javascript() to auto-generate <link> and <script> tags for the datetime picker.<script src="{{ asset('js/jquery.min.js') }}"></script>
<script src="{{ asset('js/bootstrap.min.js') }}"></script>
{{ form_javascript(form) }}
@Assert\DateTime).format in pickerOptions matches Symfony’s expected format (e.g., YYYY-MM-DD for database storage).$builder->add('startTime', DatetimeType::class, [
'pickerOptions' => [
'minView' => 'hour',
'maxView' => 'decade',
],
]);
$builder->add('appointment', DatetimeType::class, [
'pickerOptions' => [
'startDate' => (new \DateTime())->format('m/d/Y'),
'endDate' => (new \DateTime('+1 month'))->format('m/d/Y'),
],
]);
Asset Paths:
assets:install is run after updating dependencies.public/ as the target directory (not web/).Date Format Mismatch:
format in pickerOptions must align with Symfony’s expected format. For example:
YYYY-MM-DD HH:mmDateTimeType with widget: single_text and format: 'Y-m-d H:i'.Locale Inconsistencies:
Intl) and JavaScript (Bootstrap). Override locales in js/locales/ if needed (as noted in the README).Dependency Conflicts:
parent() to include base assets:
{% block javascripts %}
{{ parent() }}
{{ form_javascript(form) }}
{% endblock %}
$ is not defined → jQuery not loaded).{{ dump(form.vars) }} in Twig to inspect form variables and options.Custom Templates:
Override the form theme (e.g., templates/form/div_datetime_widget.html.twig) to modify the HTML structure.
Dynamic Options: Pass dynamic options via form events:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
$form->add('dynamicDate', DatetimeType::class, [
'pickerOptions' => ['startDate' => $data->getMinDate()],
]);
});
Localization:
Extend the bundle’s locale files (e.g., Resources/public/js/locales/bootstrap-datetimepicker.fr.js) for custom translations.
data-toggle or conditional Twig blocks).# config/packages/twig.yaml
twig:
cache: '%kernel.cache_dir%/twig'
How can I help you explore Laravel packages today?