grailsInvas0r
grailsInvas0r

Reputation: 655

play 2.0 how to access error messages from Model

I have defined some Error Messages and my Question is, how to access them correctly?!

Model:

@Entity
public class Task extends Model {

    @Constraints.Required(message="Start Date is required")
    public Date start;

}

Now, if you got a validation error in my controller save() method as you can see:

public class Tasks extends Controller {

    public static Result save() {
        Form<Task> filledForm = taskForm.bindFromRequest();
        if (filledForm.hasErrors()) {
            return badRequest(
                    create.render("create", filledForm)
        }
    }

}

and now, in my view:

@if(taskForm.hasErrors) {
        <div class="alert alert-error">
            @taskForm.errors
        </div>
}

I get the error message on the screen like this:

{name=[ValidationError(start, Start Date is required,[])]}

So, how can I access now the "Start Date is required"-message directly? I think the "@taskForm.erros" is a map, but I'm not sure.

Thank you very much.

Cheers,

Marco

Upvotes: 1

Views: 836

Answers (1)

Leonard Punt
Leonard Punt

Reputation: 1051

The errors() method indeed returns a Map, to be specific a Map<String, List<ValidationError>>.

To access the message directly use the method error(String key). So in this case use @taskForm.get("start")

Upvotes: 1

Related Questions