PHPackages                             silverstripe/spamprotection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. silverstripe/spamprotection

ActiveSilverstripe-vendormodule[Utility &amp; Helpers](/categories/utility)

silverstripe/spamprotection
===========================

Spam protection module for SilverStripe.

5.0.0(1y ago)44973.7k—9.8%37[3 issues](https://github.com/silverstripe/silverstripe-spamprotection/issues)20BSD-3-ClausePHPPHP ^8.3CI passing

Since Mar 7Pushed 5mo ago14 watchersCompare

[ Source](https://github.com/silverstripe/silverstripe-spamprotection)[ Packagist](https://packagist.org/packages/silverstripe/spamprotection)[ RSS](/packages/silverstripe-spamprotection/feed)WikiDiscussions 5 Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (64)Used By (20)

SpamProtection Module
=====================

[](#spamprotection-module)

[![CI](https://github.com/silverstripe/silverstripe-spamprotection/actions/workflows/ci.yml/badge.svg)](https://github.com/silverstripe/silverstripe-spamprotection/actions/workflows/ci.yml)[![Silverstripe supported module](https://camo.githubusercontent.com/9b7e93d393a01f6d3091fb30983b870aa863ef076858115faaa1c74b995854ec/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73696c7665727374726970652d737570706f727465642d3030373143342e737667)](https://www.silverstripe.org/software/addons/silverstripe-commercially-supported-module-list/)

Installation
------------

[](#installation)

```
composer require silverstripe/spamprotection
```

Maintainer Contact
------------------

[](#maintainer-contact)

- Saophalkun Ponlu &lt;phalkunz (at) silverstripe (dot) com&gt;
- Will Rossiter &lt;will (at) fullscreen (dot) io&gt;

Documentation
-------------

[](#documentation)

This module provides a generic, consistent API for adding spam protection to your Silverstripe Forms. This does not provide any spam protection out of the box. For that, you must also download one of the spam protection implementations. Currently available options are:

- reCAPTCHA v2 (two implementations: [one](https://github.com/chillu/silverstripe-recaptcha), [two](https://github.com/UndefinedOffset/silverstripe-nocaptcha))
- [MathSpamProtection](https://github.com/silverstripe/silverstripe-mathspamprotection)
- [Akismet](https://github.com/silverstripe/silverstripe-akismet)
- [Mollom](https://github.com/silverstripe-archive/silverstripe-mollom)
- [Cloudflare Turnstile](https://github.com/silverstripe-terraformers/turnstile-captcha/)

As a developer you can also provide your own protector by creating a class which implements the `\SilverStripe\SpamProtection\SpamProtector` interface. More on that below.

Configuring
-----------

[](#configuring)

After installing this module and a protector of your choice (i.e mollom) you'll need to rebuild your database by running `sake db:build --flush` and set the default protector via SilverStripe's config system. This will update any Form instances that have spam protection hooks with that protector.

*mysite/\_config/spamprotection.yml*

```
---
name: mycustomspamprotection
---
SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension:
  default_spam_protector: MollomSpamProtector
```

To add spam protection to your form instance call `enableSpamProtection`.

```
// your existing form code
$form = new Form(/* .. */);
$form->enableSpamProtection();
```

The logic to perform the actual spam validation is controlled by each of the individual `SpamProtector` implementations since they each require a different implementation client side or server side.

### Options

[](#options)

`enableSpamProtection` takes a hash of optional configuration values.

```
$form->enableSpamProtection(array(
    'protector' => MathSpamProtector::class,
    'name' => 'Captcha'
));
```

Options to configure are:

- `protector`: a class name string or class instance which implements `\SilverStripe\SpamProtection\SpamProtector`. Defaults to your `SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension.default_spam_protector` value.
- `name`: the form field name argument for the Captcha. Defaults to `Captcha`.
- `title`: title of the Captcha form field. Defaults to `''`
- `insertBefore`: name of existing field to insert the spam protection field prior to
- `mapping`: an array mapping of the Form fields to the standardised list of field names. The list of standardised fields to pass to the spam protector are:

```
title
body
contextUrl
contextTitle
authorName
authorMail
authorUrl
authorIp
authorId

```

Defining your own `SpamProtector`
---------------------------------

[](#defining-your-own-spamprotector)

Any class that implements `\SilverStripe\SpamProtection\SpamProtector` and the `getFormField()` method can be set as the spam protector. The `getFormField()` method returns the `FormField` to be inserted into the `Form`. The `FormField` returned should be in charge of the validation process.

```
