You can get a better idea of ββthis by checking out the form source code here.
Form defines the apply method:
def apply(key: String): Field = Field( this, key, constraints.get(key).getOrElse(Nil), formats.get(key), errors.collect { case e if e.key == key => e }, data.get(key))
This, as stated in the document, returns any field, even if it does not exist. And Field has an errors member that returns Seq[FormError] :
So you can do something like this (for Seq[FormError] ):
eventForm("name").errors.foreach { error => <div>@error.message</div> }
Or (for Option[FormError] )
eventForm("name").error.map { error => <div>@error.message</div> }
Or you can use Form errors :
def errors(key: String): Seq[FormError] = errors.filter(_.key == key)
And get all the errors of this key. Like this (for Seq[FormError] ):
eventForm.errors("name").foreach { error => <div>@error.message</div> }
Or (for Option[FormError] )
eventForm.error("name").map { error => <div>@error.message</div> }
If you need more information, check the source code. It is well written and well commented.
Hooray!
EDIT:
As biesior noted: in order to show humanoid messages with different languages, you should check how the I18N game works here
To be thorough, you probably have to deal with the I18N. It is not difficult to make it all work. After reading the documentation, you may still find yourself consumed a little. I will give you a little push. Add the messages file to the conf folder and you can copy its contents from here . This way you get more control over the default messages. Now, in your opinion, you should do something like this:
eventForm.errors("name").foreach { error => <div>@Messages(error.message, error.args: _*)</div> }
For example, if error.message were error.invalid , it would display the message previously defined in the conf/messages Invalid value file. args define some arguments that your error message may handle. For example, if you were handling error.min , arg might be the minimum required value. In your message, you just need to follow the pattern {n} , where n is the order of your argument.
Of course, you can define your own messages as follows:
error.futureBirthday=Are you sure you're born in the future? Oowww hay, we got ourselves a time traveler!
And in your controller, you can test your form like this (only one line of code to show you its feeling)
"year" -> number.verifying("error.furtureBirthday", number <= 2012)
If you want to play with languages, just follow the documentation.
Hi again!