Installation:
composer require austral/form-bundle
Ensure Austral\EntityBundle and Austral\ToolsBundle are also installed (required dependencies).
Bundle Registration:
Add to config/bundles.php:
Austral\FormBundle\FormBundle::class => ['all' => true],
First Use Case:
Create a form type extending Austral\FormBundle\Form\AbstractType:
use Austral\FormBundle\Form\AbstractType;
use Austral\FormBundle\Form\Field\FieldInterface;
class MyFormType extends AbstractType
{
public function buildForm(): void
{
$this->addField('text', 'username', [
'label' => 'Username',
'required' => true,
]);
}
}
Rendering: Use in a controller:
use Austral\FormBundle\Form\FormFactory;
public function index(FormFactory $formFactory)
{
$form = $formFactory->create(MyFormType::class);
return $this->render('my_template.html.twig', ['form' => $form->createView()]);
}
Key Files:
src/Form/AbstractType.php: Base class for form types.src/Form/Field/: Field implementations (e.g., TextField, SelectField).templates/: Twig templates for fields (override in your project).Field Definition:
Use addField() in buildForm() with:
text, select, fieldset, etc.).label, required, autocomplete, collapse).Example with nested fieldset:
$this->addField('fieldset', 'address', [
'fields' => [
'text' => 'street',
'text' => 'city',
'select' => 'country' => ['choices' => ['US', 'CA']],
],
'collapse' => true, // Added in v3.0.1
]);
Dynamic Fields:
Use addDynamicField() for conditional fields:
$this->addDynamicField('checkbox', 'subscribe', [
'options' => ['label' => 'Subscribe to newsletter'],
'condition' => ['field' => 'newsletter_consent', 'value' => true],
]);
Mapping to Entities:
Extend Austral\FormBundle\Form\Mapper\AbstractMapper to handle entity hydration:
class UserMapper extends AbstractMapper
{
protected function mapDataToEntity($data, User $entity): void
{
$entity->setUsername($data['username']);
}
}
Bind mapper in buildForm():
$this->setMapper(new UserMapper());
Validation Integration:
Leverage Symfony’s validator via options:
$this->addField('email', 'email', [
'constraints' => [new NotBlank(), new Email()],
]);
Twig Integration:
Override templates in templates/AustralFormBundle/ (e.g., field_text.html.twig).
Access form data via form.vars:
{{ form_label(form) }}
{{ form_widget(form, { 'attr': { 'autocomplete': form.vars.autocomplete ? 'on' : 'off' } }) }}
Autocomplete Handling:
Set autocomplete option (default: on for text/email, off for checkbox/select/radio unless specified):
$this->addField('text', 'password', ['autocomplete' => 'new-password']);
Austral\EntityBundle\Annotation\Mapper to auto-register mappers.form.pre_submit or form.post_map via Symfony’s event system.sortable option (v3.0.1+) for drag-and-drop reordering in fieldsets:
$this->addField('fieldset', 'items', ['sortable' => true]);
Austral\FormBundle\Form\Field\AbstractField for reusable components.Template Path Changes (v3.1.0):
templatePath option was renamed to template in v3.1.0. Update overrides if using custom templates.// Old (v3.0.x)
$this->addField('text', 'name', ['templatePath' => '@MyBundle/field.html.twig']);
// New (v3.1.0+)
$this->addField('text', 'name', ['template' => '@MyBundle/field.html.twig']);
Autocomplete Defaults:
autocomplete: off (v3.1.0+). Explicitly set autocomplete: on if needed:
$this->addField('checkbox', 'agree', ['autocomplete' => 'on']);
Fieldset Collapse:
collapse option (v3.0.1+) requires JavaScript for toggling. Ensure your frontend supports it:
// Example using jQuery (if not using Austral’s JS)
$('.austral-fieldset-collapsible').on('click', function() {
$(this).next().toggle();
});
Sortable Fields:
new Sortable(document.querySelector('.austral-sortable'), {
animation: 150,
});
Mapper Binding:
setMapper() will result in unmapped data. Always bind mappers in buildForm():
$this->setMapper(new MyMapper());
Dynamic Fields Conditions:
'condition' => ['field' => 'user.profile.active', 'value' => true],
Form Data Dumping:
Use dump($form->getData()) in controllers to inspect submitted data.
Template Debugging:
Enable Twig debug mode and check for missing templates in var/cache/dev/.
Mapper Issues:
dd($this->getMapper()->mapDataToEntity($data, $entity)) to debug mapping.Validation Errors: Access errors via:
$errors = $form->getErrors(true, false);
Custom Field Types:
Extend AbstractField and register via service:
# config/services.yaml
services:
App\Form\Field\CustomField:
tags: [austral.form.field_type]
Field Options:
Add custom options to AbstractField and handle in buildView()/finishView().
Event Listeners:
Subscribe to form.events:
$dispatcher->addListener('form.pre_submit', function (FormEvent $event) {
$form = $event->getForm();
// Modify form data before submission
});
Override Default Templates:
Copy templates from vendor/austral/form-bundle/templates/ to templates/AustralFormBundle/ and customize.
Configuration:
Override bundle config in config/packages/austral_form.yaml:
austral_form:
default_options:
autocomplete: 'on' # Global default
AddressFieldsetType).addDynamicField with condition for multi-step forms.enabled options to hide sections.Austral\FormBundle\Test\FormTestCase for form unit tests.How can I help you explore Laravel packages today?