Custom constrained field constructor

I wrote my own field constructor as shown below:

@(elements: helper.FieldElements)

@*****************************************************
* Generate input according to Twitter Bootsrap rules *
******************************************************@
<div class="control-group @if(elements.hasErrors) {error}">
    <label class="control-label" for="@elements.id">@elements.label @if(elements.field.constraints.map(c => c._1).contains("constraint.required")) {*}</label>
    <div class="controls">
        @elements.input
        <span class="help-inline">@elements.errors(elements.lang).mkString(", ")</span>
        <span class="help-block">@elements.infos(elements.lang).mkString(", ")</span>
    </div>
</div>

      

The idea is to add a star symbol *

after the label of the required elements.

It works fine with fields that have a mapping nonEmptyText

in the form definition, but my main concern is to do the same with the required email fields: whether a field can be defined as email

or optional(email)

on a form, the same constraint applies constraint.email

.

So how can I find the difference in my field constructor and add an asterisk only to the email fields I need?

+3


source to share


1 answer


Especially for email:

You can define your email address in the Forms file as follows:



import play.api.data.Forms.email
import play.api.data.validation.Constraints.nonEmpty

"email" -> email.verifying(nonEmpty)

      

This will add the constraint you expect: "constraint.required"

+1


source







All Articles