Implementing MathSpamProtection

MathSpamProtection module

Tthis module first came to exist in may 2010. Since then the folk at SilverStripe have released an alpha version of a whole new SpamProtection module, that no longer uses the built-in SpamProtection class, but has all its (improved?) code built into the SpamProtectionField, as it should be. I'll still leave this one here in support of my own implementations that use it, and others that might still have it running.

Download (mathspamprotection.tar.gz)

Using the SpamProtectionManager makes MathSpamProtection available for the UserForms module as well. For the SpamprotectionManager to support MathSpamProtection you need two classes: The MathSpamProtectorField class that generates a TextField-like field for the MathSpamProtection, and the MathSpamProtector class that will implement the field. Next you need to point the SpamprotectionManager to the right protector class in mysite/_config.php.

The two class-files can be located anywhere - mysite/code would be fine, but you might need to do a /?flush=1 on the page.

  • MathSpamProtectorField.php - The MathSpamProtectorField extends the SpamProtectorField and is loosly based on the TextField. Validation in turn is based on how it's done in the EmailField. Reminder: look into Ajax forms...
  • MathSpamProtector.php - returns the MathSpamProtectorField


  • These files need the SilverStripe spamprotection module.
  • Copy the module to your SilverStripe root.
  • Add the following to your _config.php:
// enable MathSpamProtection

To add MathSpamprotection on a custom form, use the SpamProtectorManager::update_form() function to add  the formfield, like you would with any other SpamProtection field. So, supposing you are creating a ContactForm, you'd do something like this:

function ContactForm() {

   	// define fields, actions and stuff...

	$form = new Form($this, 'ContactForm', $fields, $actions, $validator);

	// enable MathSpamProtection
	if(class_exists('SpamProtectorManager')) {

	// now return the Form object
	return $form;


Currently the native SilverStripe MathSpamProtection doesn't support multiple forms on the same page. Rewrite the module so it uses sessionMessage() with the right form name to store the results.


  • Sure, drop me a line using the contactform :-) here, and I'll get back to you...

    Verstuurd door Martine, 16/01/2012 11:14am (8 jaar geleden)

  • Thanks for the reply. But I have another twist.

    Let me explain in more detail, I have created a contact form widget and am trying to add your math spam protection to it but it doens't seem to be adding. I can send you the php for my widget if that helps.

    Again any help would be appreciated

    Verstuurd door Arun, 16/01/2012 10:35am (8 jaar geleden)

  • Hi,

    Use the SpamProtectorManager::update_form() function to add MathSpamProtection to your custom form, after you enable MathSpamProtection in your _config.php.

    I'v added an example to the bottom of the article (and to the module's README)

    Verstuurd door Martine, 13/01/2012 11:04pm (8 jaar geleden)

  • Hi,

    I just wanted to know one thing, how can I implement this math spam protection on a custom form?

    I've tried adding it as a new field:
    new MathSpamProtectionField('spamCheck')

    but it doesn't seem to work.

    Any help would be appreciated.

    Verstuurd door Arun, 13/01/2012 6:50pm (8 jaar geleden)

Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina