Install the Bundle
Add to composer.json:
"require": {
"vworldat/contact-form-bundle": "^1.0"
}
Enable in config/bundles.php:
return [
// ...
Vworldat\ContactFormBundle\VworldatContactFormBundle::class => ['all' => true],
];
Configure the Bundle
Update config/packages/vworldat_contact_form.yaml (or create it):
vworldat_contact_form:
email:
from: 'contact@example.com'
to: 'team@example.com'
subject: 'New Contact Form Submission'
database:
enabled: true # Save submissions to Propel
First Use Case: Embed a Basic Form
In a Twig template (e.g., contact.html.twig):
{{ render(controller('VworldatContactFormBundle:Default:form')) }}
This auto-generates a form with validation (name, email, message).
Custom Form Fields Extend the default form type by creating a custom service:
# config/services.yaml
services:
App\ContactForm\CustomFormType:
tags: ['contact_form.form_type']
Define fields in buildForm():
$builder->add('phone', TextType::class, ['required' => false]);
Dynamic Recipients Override the email recipient via a listener:
// src/EventListener/ContactFormListener.php
public function onSubmit(ContactFormEvent $event) {
$event->setRecipient('support@' . $event->getFormData()->getDomain());
}
Register in services.yaml:
services:
App\EventListener\ContactFormListener:
tags: ['kernel.event_listener', { event: 'contact_form.submit', method: 'onSubmit' }]
Propel Integration
Ensure Propel schema matches the form fields. Example schema (schema.xml):
<table name="contact_form_submissions" phpName="ContactFormSubmission">
<column name="name" type="VARCHAR" size="255" required="true" />
<column name="email" type="VARCHAR" size="255" required="true" />
<!-- Add custom fields -->
</table>
Twig Embedding Pass options to the form:
{{ render(controller('VworldatContactFormBundle:Default:form', {
'action': path('app_contact_submit'),
'form_options': { 'attr': { 'class': 'my-custom-form' } }
})) }}
Propel Schema Mismatch
php app/console propel:build --all after schema changes.Email Configuration
from email must be valid or emails may fail silently.Validation Overrides
# config/packages/vworldat_contact_form.yaml
vworldat_contact_form:
validation:
email: ~ # Disables email validation
CSRF Token Issues
{{ form_start(form, { attr: { 'novalidate': 'novalidate' } }) }}
{{ form_widget(form._token) }}
debug:event-dispatcher to verify listeners are attached:
php bin/console debug:event-dispatcher contact_form
config/packages/propel.yaml:
propel:
logging: true
Custom Form Actions Override the controller:
# config/routes.yaml
app_contact_submit:
path: /contact
controller: App\Controller\CustomContactController::submitAction
Extend Vworldat\ContactFormBundle\Controller\DefaultController.
Async Processing Use a message queue (e.g., Symfony Messenger) to decouple form submission from email sending:
// src/Message/HandleContactForm.php
public function __invoke(ContactFormMessage $message) {
$this->mailer->send($message->getEmail());
}
Multi-Language Support Translate form labels/placeholders via translation files:
# translations/messages.en.yaml
contact_form:
name: 'Your Name'
email: 'Your Email'
How can I help you explore Laravel packages today?