I came across the below method here
implicit def toLazyOr[T](cons: Constraint[T]) = new {
def or(other: Constraint[T]) = Constraint { field: T =>
cons(field) match {
case Valid => other(field)
case Invalid => Invalid
}
}
}
I defined toLazyOr method and then I am trying to use it in my code. But, I am not sure how do I use this.
I tried:
val adminForm = Form(
mapping(
"email" -> (email verifying toLazyOr(nonEmpty, minLength(4)) )
)
And:
val adminForm = Form(
mapping(
"email" -> (email verifying toLazyOr(nonEmpty or minLength(4)) )
)
Both are not working and my scala knowledge for the moment is very basic.
Please help.
Without knowing very much about play:
If the implicit conversion is in scope, the following should work:
val adminForm = Form(
mapping(
"email" -> (email verifying (nonEmpty or minLength(4)))
))
That's the thing about implicit conversions: You don't have to call them explicitly. See this answer for more information about where the compiler is looking for implicits.
Related
I have a problem with defining a custom Constraint on my form. This is my form:
val form = Form(
mapping(
"field" -> boolean
)(MyForm.apply)(MyForm.unapply)
If I do the following:
val form = Form(
mapping(
"field" -> boolean
)(MyForm.apply)(MyForm.unapply).verifying("my constraint", f => f.field == true)
then it works fine. However, if I try to use the following custom defined Constraint , then a null pointer exception is thrown and I do not knowing why:
val form = Form(
mapping(
"field" -> boolean
)(MyForm.apply)(MyForm.unapply).verifying(myconstraint)
val myconstraint : Constraint[MyForm] = Constraint("myconstraint")({
form =>
if(form.field == true){
Valid
}else {
Invalid(Seq(ValidationError("problem")))
}
})
I tried to use println to debug but I nothing is printed in the console.
Try moving myconstraint definition above the form definition like so
val myconstraint : Constraint[MyForm] = Constraint("myconstraint")({ ...
...
val form = Form( ...
due to possible forward reference problem where we end up referencing an uninitialised value.
I'm brand new to Play!, and I'm trying to migrate my existing website from cakePHP to Play!.
The problem I'm facing is about form validation.
I defined a case class User, representing the users of my website :
case class User(
val id: Long,
val username: String,
val password: String,
val email: String
val created: Date)
(There are some more fields, but these ones suffice to explain my problem)
I would like my users to be able to create an account on my website, using a form, and I would like this form to be validated by Play!.
So, I created the following action :
def register = Action {
implicit request =>
val userForm = Form(
mapping(
"id" -> longNumber,
"username" -> nonEmptyText(8),
"password" -> nonEmptyText(5),
"email" -> email,
"created" -> date)(User.apply)(User.unapply))
val processedForm = userForm.bindFromRequest
processedForm.fold(hasErrors => BadRequest("Invalid submission"), success => {
Ok("Account registered.")
})
}
Obviously, I do not want the user to fill the id or the creation date by himself in the form. So my question is : what should I do ?
Should I define a new "transition model" containing only the fields that are actually provided to the user in the form, and transform this intermediary model into the complete one before inserting it into my database ?
That is, replace my action by something like that :
def register = Action {
implicit request =>
case class UserRegister(
username: String,
password: String,
email: String)
val userForm = Form(
mapping(
"username" -> nonEmptyText(8),
"password" -> nonEmptyText(8),
"email" -> email)(UserRegister.apply)(UserRegister.unapply)
val processedForm = userForm.bindFromRequest
processedForm.fold(hasErrors => BadRequest("Invalid submission"), success => {
val user = User(nextID, success.username, success.password, success.email, new Date())
// Register the user...
Ok("Account created")
}
Or is there another, cleaner way to do what I want ?
I've been through many tutorials and the book "Play for Scala", but in the only examples that I found, the models were fully filled by the forms... I really like Play! so far, but it looks like the documentation often lacks examples...
Thank you very much for your answers !
You have a few choices:
Firstly, you could make the id and created fields Option[Long] and Option[Date] respectively. Then use a mapping like:
val userForm = Form(
mapping(
"id" -> optional(longNumber),
"username" -> nonEmptyText(8),
"password" -> nonEmptyText(5),
"email" -> email,
"created" -> optional(date)
)(User.apply)(User.unapply)
)
That would, I think, be logical, since a User with a None id would indicate that it has not yet been saved. This works well when you want to use the same form mapping to update an existing record.
Alternately, you can use ignored mappings with some arbitrary placeholder data:
val userForm = Form(
mapping(
"id" -> ignored(-1L),
"username" -> nonEmptyText(8),
"password" -> nonEmptyText(5),
"email" -> email,
"created" -> ignored(new Date)
)(User.apply)(User.unapply)
)
This is not so good when reusing the form for update operations!
Finally, don't forget that your form mappings are bound/filled by functions that turn a tuple into an object, and an object into a tuple respectively. It's just a convenient convention to use the case class User.apply and User.unapply methods since these do just that. You could write alternative factory methods on your User object to handle form instantiation:
object User {
def formApply(username: String, password: String, email: String): User =
new User(-1L, username, password, email, new Date)
def formUnapply(user: User): Option[(String,String,String)] =
Some((user.username, user.password, user.email))
}
And then user those in the Form object:
val userForm = Form(
mapping(
"username" -> nonEmptyText(8),
"password" -> nonEmptyText(5),
"email" -> email
)(User.formApply)(User.formUnapply)
)
Also, it's worth noting that the Scala forms documentation is about to get much better in 2.2.1 (and in fact it might already have rolled out here).
I'm having trouble identifying some weird behavior while using fillAndValidate on a Form. Here is the simplest use case I could think of to isolate the issue:
object Application extends Controller {
case class SimpleForm(val field: String)
val form = Form(
mapping(
"field" -> text.verifying("message", (s: String) => false)
)(SimpleForm.apply)(SimpleForm.unapply))
def index = Action { implicit request =>
println("bindFromRequest: " + form.bindFromRequest.errors)
println("fillAndValidate: " + form.fillAndValidate(SimpleForm("value")).errors)
Ok
}
}
When requesting the index page with the query ?field=value, I'm getting the following on the console:
bindFromRequest: List(FormError(field,message,WrappedArray()))
fillAndValidate: List(FormError(field,message,WrappedArray()))
Which is, as far as I know, the expected behavior.
However, when using global errors, such as verifying the mapping directly:
val form = Form(
mapping(
"field" -> text
)(SimpleForm.apply)(SimpleForm.unapply)
.verifying("message", (s: SimpleForm) => false))
The behavior I get is quite surprising:
bindFromRequest: List(FormError(,message,WrappedArray()))
fillAndValidate: List()
Why is fillAndValidate ignoring the error? Is there a way to get around this issue?
My application contain big form with 18 fields. It is processed with standard form mapping, like this:
val bigForm = Form(
mapping(
"id" -> of[ObjectId],
"title" -> text,
// And another 16 fields...
...
)
)
And all was well, but today I decided to add one more field and here comes the problem - mapping is not able to take more than 18 arguments.
What should I do then? I thinking of combining some fields into the structure, but additional structure requires additional formatter, JSON serializer and deserializer, too much work. I'm looking for a general solution, more fields are likely to appear in the future.
Another solution I'm thinking about is to handle form manually, without Form's.
Are there better solutions?
You can use nested mappings, e.g.
val bigForm = Form(
mapping(
"id" -> of[ObjectId],
"title" -> text,
"general" -> mapping(
...
)(GeneralInfo.apply)(GeneralInfo.unapply),
"advanced" -> mapping(
...
)(AdvancedInfo.apply)(AdvancedInfo.unapply)
)
)
Another possibility is using view objects and updating only the part that was submitted (e.g. via separate forms or AJAX):
val generalForm = Form(
mapping(
"title" -> text,
...
)
)
def updateGeneral(id: ObjectId) = Action { implicit request =>
MyObject.findById(id).map { myObj =>
generalForm.bindFromRequest.fold(
fail => BadRequest(...),
form => {
val newObj = myObj.copy(title = form.title, ...)
MyObject.save(newObj)
Ok(...)
}
)
}.getOrElse(NotFound)
}
In the play! framework, using scala,say that i have a form such as follows:
import play.api.data._
import play.api.data.Forms._
import play.api.data.validation.Constraints._
case class User(someStringField: String, someIntField: Int)
val userForm = Form(
mapping(
"someStringField" -> text,
"someIntField" -> number verifying(x => SomeMethodThatReceivesAnIntAndReturnsABoolean(x))
)(User.apply)(User.unapply)
)
where SomeMethodThatReceivesAnIntAndReturnsABoolean is a method that performs some logic on the int to validate it.
However, i would like to be able to consider the value of the someStringField when validating the someIntField, is there a way to achieve this in play framework's forms? I know that i can do something like:
val userForm = Form(
mapping(
"someStringField" -> text,
"someIntField" -> number
)(User.apply)(User.unapply)
.verifying(x => SomeFunctionThatReceivesAnUserAndReturnsABoolean(x))
and then i would have the entire user instance available passed to the validation function. The problem with that approach is that the resulting error would be associated with the entire form instead of being associated with the someIntField field.
Is there a way to get both things, validate a field using another field and maintain the error associated to the specific field i wish to validate, instead of the entire form?
I have the same requirements with adding validation to fields depending on the value of other fields. I´m not sure how this is done in idiomatic PLAY 2.2.1 but I came up with the following solution. In this usage I´m degrading the builtin "mapping" into a simple type converter and apply my "advanced inter field" validation in the "validateForm" method. The mapping:
val userForm = Form(
mapping(
"id" -> optional(longNumber),
"surename" -> text,
"forename" -> text,
"username" -> text,
"age" -> number
)(User.apply)(User.unapply)
)
private def validateForm(form:Form[User]) = {
if(form("username").value.get == "tom" || form("age").value.get == "38") {
form
.withError("forename", "tom - forename error")
.withError("surename", "tom - surename error")
}
else
form
}
def update = Action { implicit request =>
userForm.bindFromRequest.fold({
formWithErrors => BadRequest(users.edit(validateForm(formWithErrors)))
}, { user =>
val theForm = validateForm(userForm.fill(user))
if(theForm.hasErrors) {
BadRequest(users.edit(theForm))
} else {
Users.update(user)
Redirect(routes.UsersController.index).flashing("notice" -> s"${user.forename} updated!")
}
})
}
Even though it works I´m urgently searching for a more idiomatic version...
EDIT: Use a custom play.api.data.format.Formatter in idiomatic play, more on http://workwithplay.com/blog/2013/07/10/advanced-forms-techniques/ - this lets you programmatically add errors to a form. My Formatter looks like this:
val usernameFormatter = new Formatter[String] {
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], String] = {
// "data" lets you access all form data values
val age = data.get("age").get
val username = data.get("username").get
if(age == "66") {
Left(List(FormError("username", "invalid"), FormError("forename", "invalid")))
} else {
Right(username)
}
}
override def unbind(key: String, value: String): Map[String, String] = {
Map(key -> value)
}
}
}
Registered in the form mapping like this:
mapping(
[...]
"username" -> of(usernameFormatter),
[....]
I believe what you're looking for is play.api.data.validation.Constraint.
Say you have a RegisterForm with a list of predefined cities and an otherCity field and you need either the cities or otherCity to be supplied, i.e., otherCity should be validated if cities is not provided:
case class RegisterForm(
email: String,
password: String,
cities: Option[List[String]],
otherCity: Option[String]
)
You can write a custom Constraint around this:
val citiesCheckConstraint: Constraint[RegisterForm] = Constraint("constraints.citiescheck")({
registerForm =>
// you have access to all the fields in the form here and can
// write complex logic here
if (registerForm.cities.isDefined || registerForm.otherCity.isDefined) {
Valid
} else {
Invalid(Seq(ValidationError("City must be selected")))
}
})
And your form definition becomes:
val registerForm = Form(
mapping(
"email" -> nonEmptyText.verifying(emailCheckConstraint),
"password" -> nonEmptyText.verifying(passwordCheckConstraint),
"cities" -> optional(list(text)),
"other_city" -> optional(text)
)(RegisterForm.apply)(RegisterForm.unapply).verifying(citiesCheckConstraint)
)
In this example emailCheckConstraint and passwordCheckConstraint are additional custom constraints that I defined similar to citiesCheckConstraint. This works in Play 2.2.x.
UPDATE:
Works on Play 2.3.8 as well.
if you don't mind having a prefix for you params you can group the related params:
val aForm = Form(
mapping(
"prefix" -> tuple(
"someStringField" -> text,
"someIntField" -> number
) verifying (tup => your verification)
)(tup => User.apply(tup._1, tup._2)(User.unapply...)
I use something similar just without the surrounding mapping.
You will have to adjust the apply/unapply a little and pass the arguments manually for it to compile.
The error will be registered to the "prefix" group.
I also find it weird that you cannot register errors on any field you'd like using FormError when verifying the form...
Thanks to Tom Myer, Here what I used
class MatchConstraint[A](val targetField:String, val map:(String, Map[String, String]) => A, val unmap:A => String) extends Formatter[A] {
override def bind(key: String, data: Map[String, String]): Either[Seq[FormError], A] = {
val first = data.getOrElse(key, "")
val second = data.getOrElse(targetField, "")
if (first == "" || !first.equals(second)) {
Left(List(FormError(key, "Not Match!")))
}
else {
Right(map(key, data))
}
}
override def unbind(key: String, value: A): Map[String, String] = Map(key -> unmap(value))
}
And here what's my form look like
val registerForm = Form(
mapping(
"email" -> email.verifying(minLength(6)),
"password" -> text(minLength = 6),
"passwordConfirmation" -> of(new MatchConstraint[String]("password", (key, data) => data.getOrElse(key, ""), str => str))
)(RegisterData.apply)(RegisterData.unapply)
)
I guess that they map the scala-code to JSR-Validation. There it's definitely not possible. There are some arguments to do this. Mainly that a validation should be simple and not make complex logic. How ever I still miss this too. OVal from play1 was better for me.
In the documentation:
Playframework Documentation
You can see the following code:
val userFormConstraintsAdHoc = Form(
mapping(
"name" -> text,
"age" -> number
)(UserData.apply)(UserData.unapply) verifying("Failed form constraints!", fields => fields match {
case userData => validate(userData.name, userData.age).isDefined
})
)
Mainly just verify after the unapply and you have all the fields mapped, so you can make a more complete validation.