I am making a custom field constructor in Play 2:
@(elements : helper.FieldElements)
<div class="@if(elements.hasErrors) {error}">
<label for="@elements.id">@elements.label</label>
<div class="input">
@elements.input
<span class="errors">@elements.errors.mkString(", ")</span>
<span class="help">@elements.infos.mkString(", ")</span>
</div>
</div>
I need the input element to always have a particular class "my-class". How can I do this? The FieldElements class has an args member. I tried making a copy of the elements with " my-class" appended to the value associated with '_class in args, but this did not seem to affect the input element.
One way for custom input in Play Framework is
@(elements: helper.FieldElements)
<div class="form-group @elements.args.get('_class) @if(elements.hasErrors) {has-error}" id="@elements.args.get('_id).getOrElse(elements.id + "_field")">
@if(elements.hasName) {
<label class="control-label" for="@elements.id">@elements.name</label>
} else {
<label class="control-label" for="@elements.id">@elements.label(elements.lang)</label>
}
@if(elements.input.toString().contains("class=")) {
@Html(elements.input.toString().replaceFirst("(class=[\"'])", "$1form-control "))
} else {
@Html(elements.input.toString().replaceFirst("(<\\w+ )", "$1class=\"form-control\" "))
}
@elements.errors.map { error =>
<p class="help-block">@error</p>
}
@elements.infos.map { info =>
<span class="help-block">@info</span>
}
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With