Skip to content

Commit b1088f9

Browse files
committed
a fancy verifier
1 parent 16747c1 commit b1088f9

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

samples/mini-forms/app/controllers/Comments.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@ import views._
1111
import models._
1212

1313
import play.api.i18n._
14+
import scala.js._
1415
import scala.jsvalidation.PlayLMS._
1516

1617
object Comments extends Controller {
17-
1818
val commentForm: Form[Comment] = Form(
1919
mapping(
2020
"firstname" -> nonEmptyText,
2121
"lastname" -> nonEmptyText,
2222
"company" -> optional(text),
2323
"email" -> email,
2424
"phone" -> optional(text verifying jsPattern("""[0-9.+]+""", "constraint.phone", "A valid phone number is required")),
25-
"message" -> nonEmptyText
25+
"message" -> nonEmptyText.verifying(jsConstraint("constraint.nice_message", "error.nice_message") { new { def eval(c: JS) = { import c._; (msg: Rep[String]) => {
26+
val words = msg.split(" ")
27+
def countWords(regex: String) = words.filter(regex.r.test(_)).length
28+
val hateCount = countWords("[Hh]ate|[Ss]uck")
29+
val loveCount = countWords("[Ll]ove|[Rr]ock")
30+
hateCount < loveCount
31+
}}}})
2632
)(Comment.apply)(Comment.unapply)
2733
)
2834

samples/mini-forms/conf/messages

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# Messages
22

3-
constraint.phone = e.g. 01.617.253.1000
3+
constraint.phone = e.g. 01.617.253.1000
4+
5+
constraint.nice_message = Nice message
6+
error.nice_message = There must be more 'love' than 'hate'.

0 commit comments

Comments
 (0)