O'Reilly logo

Play Framework Cookbook - Second Edition by Giancarlo Inductivo, Alexander Reelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using form validation

Play Framework provides an easy way to validate form submissions. For Play Java, we will add the validation to the model, which will check for a submitted field's length and return an error message if the validate condition is not satisfied. For Play Scala, we will add the form validation to the form object itself and define the validation parameters for each form field there.

How to do it...

For Java, we need to take the following steps:

  1. Edit the Product model, foo_java/app/models/Product.java and add the validate() method:
        public String validate() {
          if (name.length() < 3 || name.length() > 100) {
             return "Name must be at least 3 characters or a maximum of 100 characters";
       }
          return null;
        }
  2. Reload the Product form using a web ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required