Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Contactform Bundle Laravel Package

c975l/contactform-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require c975l/contactform-bundle
    
  2. Enable Routes: Add to config/routes.yaml:

    c975_l_contact_form:
        resource: "@c975LContactFormBundle/"
        type: attribute
        prefix: /
    
  3. Twig Extension: Ensure Twig\Extensions\TextExtension is enabled in config/packages/twig_extensions.yaml (Symfony 4+ auto-configures this; verify if using custom Twig setup).

  4. First Use Case: Render the form in a Twig template:

    {{ render(controller('c975LContactFormBundle:Default:contact')) }}
    

    This displays a pre-configured contact form with honeypot spam protection and auto-filled user data (if logged in).


Implementation Patterns

Core Workflow

  1. Form Rendering:

    • Use render(controller()) in Twig for the default form.
    • Override templates (e.g., templates/default/contact.html.twig) to customize UI while preserving functionality.
  2. Event-Driven Customization:

    • Extend form/email logic via events:
      // config/services.yaml
      App\EventSubscriber\ContactFormSubscriber:
          tags:
              - { name: kernel.event_subscriber }
      
    • Example subscriber to modify email recipients:
      use c975L\ContactFormBundle\Event\ContactFormEvent;
      
      class ContactFormSubscriber implements EventSubscriberInterface {
          public static function getSubscribedEvents() {
              return [
                  ContactFormEvent::FORM_SUBMIT => 'onFormSubmit',
              ];
          }
      
          public function onFormSubmit(ContactFormEvent $event) {
              $event->setRecipient('custom@example.com');
          }
      }
      
  3. Multi-Recipient Emails:

    • Dispatch ContactFormEvent::EMAIL_SEND to dynamically set recipients (e.g., route to a user’s email via a service).
  4. Spam Protection:

    • Honeypot field is auto-injected; no manual CAPTCHA needed.
    • Configure submission delay in config/packages/c975_l_contact_form.yaml:
      c975_l_contact_form:
          delay: 3000 # milliseconds
      
  5. Localization:

    • Enable multilingual routes (uncomment prefix/defaults in routes.yaml).
    • Translate form labels/emails via Symfony’s translation system.

Integration Tips

  • Validation: Extend the form type (c975LContactFormBundle\Form\ContactType) by creating a custom type and overriding the bundle’s service.
  • Email Templates: Override emails/contact.txt.twig for custom email content.
  • CSRF Protection: The form includes built-in CSRF tokens; ensure Twig’s csrf_token() is not overridden.

Gotchas and Tips

Pitfalls

  1. Outdated Codebase:

    • Last release in 2018 may lack compatibility with modern Symfony (5.4+/6.x). Test thoroughly with your version.
    • Fix: Fork the repo and update dependencies (e.g., symfony/form, symfony/mailer) if critical issues arise.
  2. Twig Extension Dependency:

    • If Twig\Extensions\TextExtension is missing, the bundle may fail silently. Verify with:
      php bin/console debug:container twig.text_extension
      
    • Fix: Install via Composer or add to config/packages/twig.yaml:
      twig:
          extensions:
              - Twig\Extensions\TextExtension
      
  3. Route Conflicts:

    • The prefix: / in routes.yaml may clash with other bundles. Use a unique prefix (e.g., /contact) or namespace routes.
  4. Event Dispatching:

    • Events like FORM_SUBMIT fire after validation but before email sending. Use EMAIL_SEND to modify the email object (e.g., attachments, subject).
  5. Honeypot Visibility:

    • The honeypot field is hidden via CSS (display: none). If spam persists, inspect the rendered HTML for visibility issues.

Debugging

  • Form Submission Issues:

    • Check Symfony’s profiler (/_profiler) for validation errors or event subscriber exceptions.
    • Enable debug mode (APP_DEBUG=true) and inspect the ContactFormEvent object in subscribers.
  • Email Not Sending:

    • Verify the mailer transport is configured (config/packages/mailer.yaml).
    • Test with a hardcoded recipient in a subscriber to isolate the issue.

Extension Points

  1. Custom Form Fields:

    • Extend the form type by creating a new service that replaces contact_type:
      # config/services.yaml
      app.contact_form.type:
          class: App\Form\CustomContactType
          tags: [form.type]
          arguments: ['c975LContactFormBundle\Form\ContactType']
      
    • Implement App\Form\CustomContactType extending the original type.
  2. Dynamic Recipients:

    • Use the ContactFormEvent::EMAIL_SEND event to fetch recipients from a database:
      $recipient = $this->userRepository->findOneBy(['id' => $event->getData()->getUserId()]);
      $event->setRecipient($recipient->getEmail());
      
  3. Attachment Support:

    • Modify the email template (emails/contact.txt.twig) to include attachments:
      {% for attachment in event.attachment %}
          {{ attachment.getContent() }}
      {% endfor %}
      
    • Pass attachments via the EMAIL_SEND event:
      $event->addAttachmentFromPath('/path/to/file.pdf');
      
  4. Logging:

    • Add logging to subscribers for debugging:
      use Psr\Log\LoggerInterface;
      
      public function __construct(LoggerInterface $logger) {
          $this->logger = $logger;
      }
      
      public function onFormSubmit(ContactFormEvent $event) {
          $this->logger->info('Form submitted', ['data' => $event->getData()->toArray()]);
      }
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky