Installation
Add the bundle to composer.json:
composer require devmachine/form-bundle:~2.0
Register the bundle in config/bundles.php:
return [
// ...
Devmachine\FormBundle\DevmachineFormBundle::class => ['all' => true],
];
Enable Form Types
Import the bundle’s routing (if needed) and ensure DevmachineFormBundle is loaded in config/packages/devmachine_form.yaml (if provided). Check for default configuration in config/packages/devmachine_form.yaml (create if missing).
First Use Case: Bootstrap Date Widget
Create a form type extending Devmachine\Form\Type\DateType:
use Devmachine\Form\Type\DateType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class MyDateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('eventDate', DateType::class, [
'widget' => 'single_text', // or 'split', 'bootstrap_datepicker'
'format' => 'yyyy-MM-dd',
]);
}
}
Bootstrap Date/Datetime Widgets
DateType/DateTimeType with widget options:
$builder->add('birthday', DateType::class, [
'widget' => 'bootstrap_datepicker',
'format' => 'dd/MM/yyyy',
'html5' => false, // Disable HTML5 validation
]);
bootstrap-datepicker JS/CSS is included in your base template.Typeahead Autocomplete
TypeaheadType with a data source (e.g., API endpoint):
$builder->add('user', TypeaheadType::class, [
'min_length' => 2,
'source' => '/api/users/autocomplete',
'display_field' => 'fullName',
'value_field' => 'id',
]);
data-src attribute in Twig:
{{ form_widget(form.user, {'attr': {'data-src': path('app_user_autocomplete')}}) }}
Dynamic Forms
form_row and form_errors in Twig:
{% for field in form %}
<div class="form-group">
{{ form_label(field) }}
{{ form_widget(field) }}
{% if form_errors(field) %}
<div class="text-danger">{{ form_errors(field) }}</div>
{% endif %}
</div>
{% endfor %}
Custom Validation
use Symfony\Component\Validator\Constraints as Assert;
$builder->add('email', TextType::class, [
'constraints' => [
new Assert\Email(),
new Assert\NotBlank(),
],
]);
bootstrap-datepicker and typeahead.js. Add to webpack.config.js or include via <script> tags.form_row with custom classes:
{{ form_row(form.field, {'attr': {'class': 'form-control'}}) }}
TypeaheadType, ensure the endpoint returns JSON:
[{"id": 1, "fullName": "John Doe"}, ...]
Missing JavaScript Dependencies
bootstrap-datepicker and typeahead.js are loaded after jQuery.404 errors on /bundles/devmachineform/....HTML5 Validation Conflicts
html5: true may override custom formats.html5: false for full control:
'widget' => 'bootstrap_datepicker',
'html5' => false,
Caching Static Assets
<script src="{{ asset('bundles/devmachineform/js/typeahead.bundle.js?v=2') }}"></script>
Deprecated Symfony Features
FormBuilder changes).{{ dump(form.vars) }} to inspect form variables.data-src attribute matches the route.Custom Widgets
Extend Devmachine\Form\Type\AbstractWidgetType to create new widgets:
class CustomWidgetType extends AbstractWidgetType
{
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'widget' => 'custom_widget',
]);
}
}
Override Default Templates
Copy templates from vendor/devmachine/form-bundle/Resources/views/Form/ to templates/Form/ to customize rendering.
Configuration Overrides
Override defaults in config/packages/devmachine_form.yaml:
devmachine_form:
date:
default_widget: 'bootstrap_datepicker'
format: 'dd/MM/yyyy'
PRE_SET_DATA/POST_SUBMIT to dynamically adjust form fields:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
if ($data->isAdmin()) {
$event->getForm()->add('adminOptions', CheckboxType::class);
}
});
mix.js('resources/js/app.js', 'public/js') and include them in your layout:
{{ mix('js/app.js') }}
How can I help you explore Laravel packages today?