diff --git a/README.md b/README.md index a1c3719..5e05652 100644 --- a/README.md +++ b/README.md @@ -66,25 +66,26 @@ implementation client side or server side. Options to configure are: -*`protector`* a class name string or class instance which implements -`SpamProtector`. Defaults to your +* `protector`: a class name string or class instance which implements `SpamProtector`. Defaults to your `FormSpamProtectionExtension.default_spam_protector` value. +* `name`: The form field name argument for the Captcha. Defaults to `Catcha`. +* `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 standardized list of field names. +The list of standardized fields to pass to the spam protector are: + * title + * body + * contextUrl + * contextTitle + * authorName + * authorMail + * authorUrl + * authorIp + * authorId -*`name`* the form field name argument for the Captcha. Defaults to `Catcha`. -*`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 standardized list of -field names. The list of standardized fields to pass to the spam protector are: - - title - body - contextUrl - contextTitle - authorName - authorMail - authorUrl - authorIp - authorId +Additional options may be specified, which may be used to activate implementation specific +features for the chosen spam protector. All of these options will be passed to the +protector constructor (see below). ## Defining your own `SpamProtector` @@ -93,16 +94,32 @@ 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. - options = $options; + } + + public function getFormField($name = null, $title = null, $value = null) + { + // CaptchaField is a imagined class which has some functionality. + // See silverstripe-mollom module for an example. + return new CaptchaField($name, $title, $value); + } + + public function setFieldMapping($fieldMapping) { + // No-op + } + } ## Using Spam Protection with User Forms diff --git a/code/extensions/FormSpamProtectionExtension.php b/code/extensions/FormSpamProtectionExtension.php index 7346d5e..f5f8654 100644 --- a/code/extensions/FormSpamProtectionExtension.php +++ b/code/extensions/FormSpamProtectionExtension.php @@ -57,7 +57,7 @@ class FormSpamProtectionExtension extends Extension } if ($protector && class_exists($protector)) { - return Injector::inst()->create($protector); + return Injector::inst()->createWithArgs($protector, array($options)); } else { return null; } diff --git a/code/interfaces/SpamProtector.php b/code/interfaces/SpamProtector.php index 729a664..ef5d927 100644 --- a/code/interfaces/SpamProtector.php +++ b/code/interfaces/SpamProtector.php @@ -14,6 +14,12 @@ interface SpamProtector { + /** + * SpamProtector constructor. + * @param array $options List of spam protection options + */ + public function __construct($options = array()); + /** * Return the {@link FormField} associated with this protector. * diff --git a/composer.json b/composer.json index 3f245c0..51cfce1 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ }, "extra": { "branch-alias": { - "dev-master": "2.0.x-dev" + "dev-master": "3.0.x-dev" } }, "license": "BSD-3-Clause" diff --git a/tests/FormSpamProtectionExtensionTest.php b/tests/FormSpamProtectionExtensionTest.php index 91111b5..390b9bf 100644 --- a/tests/FormSpamProtectionExtensionTest.php +++ b/tests/FormSpamProtectionExtensionTest.php @@ -61,10 +61,13 @@ class FormSpamProtectionExtensionTest extends SapphireTest $form = $this->form->enableSpamProtection(array( 'protector' => 'FormSpamProtectionExtensionTest_BazProtector', 'title' => 'Qux', - 'name' => 'Borris' + 'name' => 'Borris', + 'righttitle' => 'Lipsum' )); - $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title()); + $formfield = $form->Fields()->fieldByName('Borris'); + $this->assertEquals('Qux', $formfield->Title()); + $this->assertEquals('Lipsum', $formfield->RightTitle()); } public function testInsertBefore() @@ -102,9 +105,20 @@ class FormSpamProtectionExtensionTest extends SapphireTest */ class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, TestOnly { + protected $options = array(); + + public function __construct($options = array()) + { + $this->options = $options; + } + public function getFormField($name = null, $title = null, $value = null) { - return new TextField($name, $title, $value); + $field = new TextField($name, $title, $value); + if(isset($this->options['righttitle'])) { + $field->setRightTitle($this->options['righttitle']); + } + return $field; } public function setFieldMapping($fieldMapping) @@ -117,6 +131,10 @@ class FormSpamProtectionExtensionTest_BazProtector implements SpamProtector, Tes */ class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, TestOnly { + public function __construct($options = array()) + { + } + public function getFormField($name = null, $title = null, $value = null) { $title = $title ?: 'Bar'; @@ -133,6 +151,10 @@ class FormSpamProtectionExtensionTest_BarProtector implements SpamProtector, Tes */ class FormSpamProtectionExtensionTest_FooProtector implements SpamProtector, TestOnly { + public function __construct($options = array()) + { + } + public function getFormField($name = null, $title = null, $value = null) { return new TextField($name, 'Foo', $value);