Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized field constructor with constraints

I have written my own field constructor like the following:

@(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 the nonEmptyText mapping in the form definition but my main concern is to do the same with the required email fields: may the field be defined as email or optional(email) in the form, the same constraint constraint.email is used.

So how can I find the difference in my field constructor and add the star only to the required email fields?

like image 958
teemoo Avatar asked Jul 01 '26 01:07

teemoo


1 Answers

Specifically for email:

You can define your email address in the Form mapping like so:

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"

like image 102
user3439337 Avatar answered Jul 03 '26 14:07

user3439337



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!