mremi/contact-bundle
Symfony2 bundle that adds a ready-to-use contact form with optional Contact entity, configurable routing and translations. Install via Composer, enable the bundle, configure it, import routes, and optionally update your DB schema.
Installation
composer require mremi/contact-bundle
Add to AppKernel.php:
new Mremi\ContactBundle\MremiContactBundle(),
Enable Translations
Ensure framework.translator is enabled in config.yml (as per README).
First Use Case Generate a basic contact form in a controller:
use Mremi\ContactBundle\Form\Type\ContactType;
$form = $this->createForm(ContactType::class);
Resources/config/services.yml (default services).Form/Type/ContactType.php (customization entry point).Controller/ContactController.php (basic workflow).Form Submission
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$contact = $form->getData();
$em->persist($contact);
$em->flush();
}
Customizing Fields
Extend ContactType to add/remove fields:
use Mremi\ContactBundle\Form\Type\ContactType as BaseContactType;
class CustomContactType extends BaseContactType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('custom_field', TextType::class);
}
}
Email Handling
Override the default mailer service (see services.yml):
mremi_contact.mailer:
class: AppBundle\Service\CustomMailer
arguments: ['@mailer']
Validation
Add constraints to the Contact entity (e.g., NotBlank for required fields).
Twig Integration Render the form in a template:
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit">Send</button>
{{ form_end(form) }}
CSRF Protection
Ensure csrf_protection is enabled in ContactType (default behavior).
Flash Messages Use Symfony’s flash system post-submission:
$this->addFlash('success', 'Message sent!');
Missing Translations
If using default texts, ensure translator is configured and locale is set (e.g., en).
Fix: Add translator.default_locale: en to config.yml.
Form Not Submitting
csrf_token is included in the form (enabled by default).isSubmitted() and isValid() logic in the controller.Email Not Sending
mailer service is properly configured in services.yml.to email address in the mailer service.Entity Not Persisting
Ensure the Contact entity is mapped to a database table and the EntityManager is injected.
Log Form Data Dump form data before submission:
dump($form->getData());
Validate Manually Test validation separately:
$validator = $this->get('validator');
$errors = $validator->validate($contact);
Custom Mailer
Extend Mremi\ContactBundle\Mailer\Mailer to modify email templates or logic.
Entity Customization
Override the Contact entity (e.g., add created_at timestamp):
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Contact {
#[ORM\Column(type: 'datetime')]
private $createdAt;
public function __construct() {
$this->createdAt = new \DateTime();
}
}
Event Listeners
Attach listeners to the contact.send event (if supported in newer versions):
services:
app.contact_listener:
class: AppBundle\EventListener\ContactListener
tags:
- { name: kernel.event_listener, event: contact.send, method: onContactSend }
Default Recipient
The to email is hardcoded in the mailer service. Override it in services.yml:
mremi_contact.mailer:
arguments: ['@mailer', 'custom@example.com']
CSRF Token If disabled, ensure you manually add the token to the form:
{{ form_hidden(form._token) }}
How can I help you explore Laravel packages today?