Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Basic Spam Protection on a Basic Form


Go to End


8 Posts   1879 Views

Avatar
soulrolll

Community Member, 9 Posts

3 April 2015 at 4:50pm

Edited: 03/04/2015 4:50pm

Hello I am trying to integrate recaptcha spam protection into a custom form, where exactly do I implement the below code?

https://github.com/silverstripe/silverstripe-spamprotection

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

Below is my form:
http://iforce.co.nz/i/4az53any.0ux.jpg

Any help with this would be much appreciated.

Avatar
soulrolll

Community Member, 9 Posts

4 April 2015 at 5:09pm

figured it out, pretty simple :o

Avatar
Pyromanik

Community Member, 419 Posts

11 April 2015 at 1:27am

Edited: 11/04/2015 1:28am

A wild Soulrol|l appeared, but he forgot to use answer! It's not very effective...

The answer is to instead of return new Form(...),

return Form::create(...)->enableSpamProtection();

As a rule of thumb, always ::create() instead of new where the class extends from Object - there are many subtle advantages (chaining as eg. here, dependency injection, etc).

Works best in combination with something like: https://github.com/chillu/silverstripe-recaptcha
Then suddenly the map is biased to the CT's.

I've used the mollom adapter in the past too, and that's pretty good - it's a bit more fiddly to set up for much more than comment/contact based forms.

Avatar
soulrolll

Community Member, 9 Posts

13 April 2015 at 8:58am

Wouldn't it be great if that 'correct way' was properly documented in the plugin itself with some real use examples. I ended up going with the Honeypot version, I can post up the completed form if you like, cheers Pyro.

Avatar
camfindlay

Forum Moderator, 267 Posts

13 April 2015 at 9:16am

Yip always good to post code examples if you solve something on the forums as this helps others :D

Just make sure that 1) you don't post any sensitive client code ;) and 2) if the question has been solved that you prefix the original post with [solved]

Avatar
Pyromanik

Community Member, 419 Posts

13 April 2015 at 10:34pm

Yeah Soulrolll, it's an issue with things falling more out of date than anything. ClassName::create() was introduced in 3.0 to enable functionality with Injector (dependency injection class/sub-project). spamprotection & most of it's adaptors/providers are from the 2.x days.

Just to reiterate - there's nothing particularly 'wrong' about using "new ClassName" - I just like to actively encourage the use of ::create() in an effort to combat all the old (on module) docs, old tutorials and things floating around the internet, etc.
::create also provides some more syntactical sugar as such with chaining, as mentioned before. Sometimes it can make (more modern) tutorials easier to follow :)

<messsage here about open source and pull requests welcome, etc> ;)

Avatar
soulrolll

Community Member, 9 Posts

13 April 2015 at 11:23pm

This is what I ended up going with, not sure if its 100% secure but it works.

http://iforce.co.nz/i/ferurvvk.xsx.jpg

Avatar
Pyromanik

Community Member, 419 Posts

13 April 2015 at 11:46pm

I cannot remember if the default output value of the textfield is HTML safe (I think it might be though - best check).
If you find that it's not, there is the Convert helper class for that.

http://api.silverstripe.org/3.1/class-Convert.html

Other than that, seems pretty standard (apart from some redundant looking calls, but that's mostly by the by).