Install the package:
composer require alexsabur/antispam-bundle
(Note: The original package is nucleos/antispam-bundle, but the composer.json specifies alexsabur/antispam-bundle. Verify the correct package name before installation.)
Enable the bundle in config/bundles.php:
return [
// ...
Nucleos\AntiSpamBundle\NucleosAntiSpamBundle::class => ['all' => true],
];
Configure basic settings in config/packages/nucleos_antispam.yaml:
nucleos_antispam:
time:
min: 5 # Minimum seconds between form display and submission
max: 3600
honeypot:
field: 'email_address'
class: 'hidden'
First use case: Protect a form Add time protection to a form type in the controller:
$this->createForm(CustomFormType::class, null, [
'antispam_time' => true,
'antispam_time_min' => 10,
]);
Apply email obfuscation in Twig:
{{ 'contact@example.com'|antispam }}
(Output: contact[AT]example.com)
Global vs. Per-Form Configuration
global: true in nucleos_antispam.yaml to apply time/honeypot protection to all forms without manual configuration.Form Type Integration
Configure options in configureOptions():
public function configureOptions(OptionsResolver $resolver): void {
$resolver->setDefaults([
'antispam_honeypot' => true,
'antispam_honeypot_field' => 'spam_field',
'antispam_time_min' => 15,
]);
}
Dynamic Field Naming
Use a unique antispam_honeypot_field name (e.g., spam_field_{$formId}) to avoid conflicts in multi-form pages.
Rich Text Handling
Use the second parameter (true) for HTML content to preserve formatting:
{{ htmlContent|antispam(true) }}
JavaScript Decoding
Include AntiSpam.js via Webpack Encore:
// assets/js/antispam.js
import AntiSpam from 'nucleos-antispam-bundle/AntiSpam';
document.addEventListener('DOMContentLoaded', () => {
new AntiSpam('.email-class');
});
(Ensure the JS file is copied to public/ via Webpack.)
Custom CSS Classes
Configure css_class in nucleos_antispam.yaml to target specific elements:
nucleos_antispam:
twig:
mail:
css_class: 'obfuscated-email'
Leverage Symfony’s event system to extend functionality:
Listen for Form Submission
Subscribe to FormEvent::SUBMIT to log or analyze spam attempts:
// src/EventListener/AntiSpamListener.php
public function onFormSubmit(FormEvent $event) {
$form = $event->getForm();
if ($form->has('spam_field')) {
$this->logger->warning('Potential spam detected');
}
}
Custom Validation Add a validator to reject forms with invalid time/honeypot data:
use Nucleos\AntiSpamBundle\Validator\Constraints\AntiSpam;
$builder->add('your_field', null, [
new AntiSpam(['time_min' => 10]),
]);
Session Dependency
session.start() is called before form rendering.session_start() in your AppKernel or use Symfony’s built-in session handling.JavaScript Conflicts
AntiSpam.js may interfere with other JS libraries if not scoped properly.new AntiSpam('.my-app-emails');
Form Type Overrides
global: true) may conflict with explicit form configurations.null to disable global settings for a specific form:
'antispam_time' => null, // Disables global time protection
Deprecated Session Methods
Session::get() → Session::getBag('default')).Log Spam Attempts
Enable debug mode and check logs for AntiSpamBundle entries:
monolog:
handlers:
main:
level: debug
channels: ['!event']
Inspect Form HTML Verify honeypot fields are rendered with the correct CSS class:
<input type="text" name="spam_field" class="hidden" />
(Use browser dev tools to check if .hidden is applied.)
Time Protection Validation
antispam_time_min/max values are reasonable (e.g., min: 5).Custom Honeypot Fields Override the default field provider:
nucleos_antispam:
honeypot:
provider: 'app.custom_honeypot_provider'
(Implement Nucleos\AntiSpamBundle\Provider\HoneypotProviderInterface.)
Email Obfuscation Patterns Extend the Twig filter by subclassing the default provider:
// src/Twig/AntiSpamExtension.php
class CustomAntiSpamExtension extends \Nucleos\AntiSpamBundle\Twig\AntiSpamExtension {
public function getAtReplacements() {
return ['[AT]', '(AT)', 'AT']; // Add custom patterns
}
}
Register the service in services.yaml:
services:
Nucleos\AntiSpamBundle\Twig\AntiSpamExtension:
alias: 'app.custom_antispam_extension'
public: true
Custom Validation Messages
Override default error messages in config/packages/validation.yaml:
Nucleos\AntiSpamBundle\Validator\Constraints\AntiSpam:
message: 'This form was submitted too quickly. Please try again.'
YAML vs. PHP Config
nucleos_antispam.yaml) and PHP (config/packages/nucleos_antispam.php).min/max from a database).Global Settings Precedence
$resolver->setDefaults(['antispam_honeypot' => false]);
Asset Paths
AntiSpam.js) is in the assets folder. Ensure Webpack Encore copies it to public/build/:
// webpack.config.js
Encore
.addEntry('antispam', './assets/js/antispam.js')
.copyFiles({
from: './assets/js/AntiSpam.js',
to: 'build/[name].js',
});
How can I help you explore Laravel packages today?