Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play framework 2 Constraints client side validation

I am currently using Play Framework and was wondering if there is a helper template out there (even by a 3rd party) that will add the HTML5 attributes (e.g. required) based on the model annotations.

For example on my model

public class MyModel {
   Constraints.Required(message="Required")
   public String name;
}

on my View.Scala.html

@helper.inputText(
            field = myModelForm("name"),
            '_label -> "Name",
            '_class -> "form-control",
            '_showConstraints -> false,
            '_showErrors -> true)

Is there an existing set of helper templates or 3rd party plugin that will add the required attribute on the HTML element?

I thought about creating my own helper templates but I'm not very familiar with Scala and do not know how to use reflection to check for attributes. If you are familiar with this syntax, please help! This would give me the flexibility to use Angular validation or jQuery validation but it's not a requirement at this point.

Thank you in advance!

like image 509
Amir Avatar asked Apr 24 '15 20:04

Amir


1 Answers

There is a module for this: https://github.com/loicdescotte/Play2-HTML5Tags

I just tested it occasionally but seems it does what you need (at least with most popular constraints like Required MinLength etc. Also allows to use input fields with email or number type.

If it won't satisfy you I'm pretty sure that you can use it as reference and create custom solution.

like image 123
biesior Avatar answered Oct 25 '22 15:10

biesior