In BuildConfig.groovy insert this line:
compile ":simple-captcha:1.0.0"
<img src="${createLink(controller: 'simpleCaptcha', action: 'captcha')}"/><br/>
<input type="text" name="captcha"><br/>
<input type="submit" value="Register">
There will appear an image with capthca and the entered value will be passed as params.captcha.
In the controller that your <g:form> refers to put this code:
Outside of any action:
def simpleCaptchaService
boolean captchaValid = simpleCaptchaService.validateCaptcha(params.captcha)
if (captchaValid) {
def user = new User(userName:params.userName)
user.save()
...
} else {
flash.message = "Wrong capthca"
redirect(controller:"user", action:"login")
}
There also is a configuration closure that you can put into the Config.groovy
simpleCaptcha {
// font size used in CAPTCHA images
fontSize = 30
height = 200
width = 200
// number of characters in CAPTCHA text
length = 6
// amount of space between the bottom of the CAPTCHA text and the bottom of the CAPTCHA image
bottomPadding = 16
// distance between the diagonal lines used to obfuscate the text
lineSpacing = 10
// the charcters shown in the CAPTCHA text must be one of the following
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// this param will be passed as the first argument to this java.awt.Font constructor
// http://docs.oracle.com/javase/6/docs/api/java/awt/Font.html#Font(java.lang.String,%20int,%20int)
font = "Serif"
}
Nice Plugins
ReplyDelete