Play Frameworkで、@Constraints.MaxLengthの値をviewに引き継ぐ
modelにバリデーション情報書いてるのにviewでももう一回書く、というのがどうしても気持ち悪かったのでヘルパー書きました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(field: play.api.data.Field, args: (Symbol,Any)*)(implicit handler: views.html.helper.FieldConstructor, lang: play.api.i18n.Lang) | |
@inputType = @{ args.toMap.get('type).map(_.toString).getOrElse("text") } | |
@maxLen = @{field.constraints.filter(_._1=="constraint.maxLength").map(_._2)} | |
@views.html.helper.input(field, args.filter(_._1 != 'type):_*) { (id, name, value, htmlArgs) => | |
<input type="@inputType" id="@id" name="@name" value="@value"@if(!maxLen.isEmpty){ maxlength="@maxLen"} @toHtmlArgs(htmlArgs)> | |
} |