captcha

102 views
Skip to first unread message

Alexandre

unread,
Aug 21, 2009, 12:08:42 PM8/21/09
to play-framework
Is there any sample about Image captcha helper?

Guillaume Bort

unread,
Aug 21, 2009, 12:21:52 PM8/21/09
to play-fr...@googlegroups.com
Not yet. It will be part of the new document for the 1.0 release, that
I'm currently writing.
Here a sample from my draft play guide:

Action generating the captcha:

public static void captcha() {
Images.Captcha captcha = Images.captcha();
String code = captcha.getText("#E4EAFD");
Cache.set(session.getId() + "captcha", code, "10mn");
renderBinary(captcha);
}

Action that validate the captcha:

public static void postComment(Long postId, String author, String
content, String code) {
Post post = Post.findById(postId);
// Input validation
validation.required(author).message("All fields are required");
validation.required(content).message("All fields are required");
validation.equals(code, Cache.get(session.getId() +
"captcha")).message("Invalid code. Please type it again");
if(validation.hasErrors()) {
render("Application/show.html", post);
}
// Ok, add the comment
post.addComment(author, content);
flash.success("Thanks for posting %s !", author);
show(postId);
}

And part of the form template:

#{form @Application.postComment(post.id)}

#{ifErrors}
<p class="error">
${errors[0]}
</p>
#{/ifErrors}

<p>
<label for="author">Your name: </label>
<input type="text" name="author" id="author" value="${params.author}" />
</p>
<p>
<label for="content">Your message: </label>
<textarea name="content" id="content">${params.content}</textarea>
</p>
<p>
<label for="code">Type the following code: </label>
<img id="captcha" src="@{Application.captcha()}" />
<input type="text" name="code" id="code" />
</p>
<p>
<input type="submit" value="Submit your comment" />
</p>
#{/form}

Guillaume Bort

unread,
Aug 21, 2009, 12:23:44 PM8/21/09
to play-fr...@googlegroups.com
By the way it works with a recent version of Play. The 1.0-RC1 will be fine.

Alexandre

unread,
Aug 21, 2009, 12:26:09 PM8/21/09
to play-framework
Thanks Guillaume.


On 21 Ago, 18:23, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> By the way it works with a recent version of Play. The 1.0-RC1 will be fine.
>
Reply all
Reply to author
Forward
0 new messages