nzo / honeypot-bundle
A honeypot field to reduce automated spam in Symfony forms
Package info
github.com/nayzo/NzoHoneypotBundle
Type:symfony-bundle
pkg:composer/nzo/honeypot-bundle
Requires
- php: >=8.1
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/form: ^6.4 || ^7.0 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
- symfony/options-resolver: ^6.4 || ^7.0 || ^8.0
- symfony/validator: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.68
- phpstan/phpstan: ^1.12 || ^2.0
- phpunit/phpunit: ^9.6
- rector/rector: ^1.2 || ^2.0
- symfony/filesystem: ^6.4 || ^7.0 || ^8.0
- symfony/security-csrf: ^6.4 || ^7.0 || ^8.0
- symfony/twig-bundle: ^6.4 || ^7.0 || ^8.0
- symfony/var-dumper: ^6.4 || ^7.0 || ^8.0
Suggests
- symfony/twig-bundle: Provides Symfony's Twig form helpers for rendering the field in a template
Provides
None
Conflicts
None
Replaces
None
README
A Symfony bundle that helps reduce form spam by adding a text field that people do not see but some bots fill in.
By default, the field is not mapped to your data. If it contains anything, even just spaces, Symfony rejects the submission with a generic form error.
Which version do I need?
| Your application | Bundle version | Install |
|---|---|---|
| Symfony 6.4, 7 or 8 on PHP 8.1 or later | 2 | composer require nzo/honeypot-bundle |
| Symfony 4.4 or 5 on PHP 7.1.3 to 7.4 | 1 | composer require nzo/honeypot-bundle:^1.2 |
Use the minimum PHP version required by your Symfony release if it is higher. Version 1 listed Symfony 6 as a dependency option, but its PHP 7 constraint was incompatible with Symfony 6. If you are on Symfony 6.0 to 6.3, upgrade to at least 6.4 to install this bundle.
The documentation for version 1 is on the 1.x branch.
Coming from version 1? See UPGRADE-2.0.md.
Installation
composer require nzo/honeypot-bundle
Symfony Flex registers the bundle automatically. Without Flex, add it to config/bundles.php:
return [ // ... Nzo\HoneypotBundle\NzoHoneypotBundle::class => ['all' => true], ];
The bundle has no global settings. If your app has disabled Symfony Forms or validation, enable them:
# config/packages/framework.yaml framework: form: true validation: true
Usage
Add HoneypotType to a form with a name that is not used by a real field:
namespace App\Form; use Nzo\HoneypotBundle\Form\Type\HoneypotType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\FormBuilderInterface; class ContactType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('email', EmailType::class) ->add('message', TextareaType::class) ->add('company_website', HoneypotType::class); } }
When rendering fields individually in Twig, render the trap with form_widget() and display errors at the form level:
{{ form_start(form) }}
{{ form_errors(form) }}
{{ form_row(form.email) }}
{{ form_row(form.message) }}
{{ form_widget(form.company_website) }}
<button type="submit">Send message</button>
{{ form_end(form) }}
If you render the whole form with form(form), Symfony renders the field for you. Only send the message after $form->isSubmitted() && $form->isValid() succeeds. A rejected submission renders the trap empty again so the user can retry.
Field options
The following options show the defaults that matter for the honeypot:
$builder->add('company_website', HoneypotType::class, [ 'honeypot_message' => 'An error has occurred, please refresh the page and try again.', 'required' => false, // Do not require input in the browser. 'mapped' => false, // Leave the trap out of your form data. 'data' => '', // Initial form data; the rendered field is always empty. 'trim' => false, // Spaces count as a filled field. 'label' => false, // Do not render a label for the trap. 'error_bubbling' => true, // Display the error on the parent form. 'attr' => [], // Add HTML attributes or override individual defaults. 'constraints' => [], // Additional constraints; the built-in Blank check stays active. ]);
honeypot_message is also the default invalid_message for malformed input. If you set error_bubbling to false, render form_errors(form.company_website) where users can see the error.
The field is a text input, not an HTML type="hidden" input. It is visually hidden by inline CSS and rendered with aria-hidden="true", tabindex="-1", inert and autocomplete="off". Extra attributes are merged with these defaults. If your Content Security Policy blocks inline styles, pass ['attr' => ['style' => false, 'class' => 'visually-hidden']] and use a visually hidden class from your stylesheet.
Custom validation groups
The Blank constraint belongs to Default and HoneypotType::HONEYPOT_GROUP (honeypot). If your form uses only custom validation groups, include the honeypot group:
$form = $this->createForm(ContactType::class, null, [ 'validation_groups' => ['contact', HoneypotType::HONEYPOT_GROUP], ]);
If you disable validation groups for a submission, the Blank constraint does not run. When using the Form component without FrameworkBundle, add Symfony's ValidatorExtension to your form factory.
Security
A honeypot catches some automated submissions. It does not prove that a submission came from a person: a bot can leave the field empty, and a browser or extension can ignore autocomplete="off". Keep CSRF protection for forms that need it and add rate limiting if spam persists.
License
This bundle is under the MIT license. See LICENSE.