お世話になってます。
今あるフォームからデータ更新する際、バリデーションを上げたいのですが
update.scala.htmlの中のhasErrorsの<span>の箇所に出したいのですが、
うまく行きません。原因が今一見えてきません。
お分かりの方おられましたら、ご教示下さい。
宜しくお願い致します。
●playerForm.java(呼び込むバリデーション)
package views.form; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import play.data.validation.ValidationError; import play.i18n.Messages; public class PlayerForm { public String id = ""; public String name =""; public String height = ""; public String blood = ""; public String birthday = ""; public String birthplace= ""; public PlayerForm() {} public PlayerForm(String id, String name, String height, String blood, String birthday, String birthplace) { this.id = id; this.name = name; this.height = height; this.blood = blood; this.birthday = birthday; this.birthplace= birthplace; } public List<ValidationError> validate(){ List<ValidationError> errors = new ArrayList<ValidationError>(); //名前 if(name == null || name.length() == 0) { errors.add(new ValidationError("name",Messages.get("Player.name.required"))); } //身長 if(StringUtils.isNotEmpty(height)) { Integer tmpH = Integer.valueOf(height); System.out.println(tmpH); if(tmpH < 1 || tmpH > 200) { errors.add(new ValidationError("height",Messages.get("Player.height.range", 1, 200))); } } //血液型 if(StringUtils.isNoneEmpty(blood) && !blood.matches("A|B|O|AB")) { errors.add(new ValidationError("blood",Messages.get("Player.blood.kind"))); } //誕生日 if(StringUtils.isNoneEmpty(birthday) && !birthday.matches("\d{4}-d{2}-d{2}\")) { errors.add(new ValidationError("birthday",Messages.get("Player.birthday.pattern"))); } //出身地Id if(StringUtils.isNotEmpty(birthplace)) { Integer tmpH = Integer.valueOf(birthplace); if(tmpH < 1 || tmpH > 47) { errors.add(new ValidationError("birthplace",Messages.get("Player.birthplace.range"))); } } if(errors.size()>0) { System.out.println("PlayerForm#validate errors"); return errors; } return null; } @Override public String toString() { return "PlayerForm [id=" + id + ", name=" + name + ", height=" + height + ", blood" + blood + ", birthday=" + birthday + ", birthplace=" + birthplace; } }
●出現エラー
[DEV] A server error occurred: Execution exception[[CompletionException: java.lang.IllegalStateException: Error(s) binding form: {"name":["名前は必須です。"]}]]
●update.scala.html
@(message: String, player: models.player, playerForm: Form[views.form.playerForm]) @import play.i18n._ @import utils.DateParser @import models.Prefecture @nitta("player Update","選手情報更新"){ @if(flash.containsKey("error")){ <div class="row"> <div class="col-md-12 well"> <div id="error-message" class="text-danger"> @flash.get("error") </div> </div> </div> } <div class="row"> <div class="col-md-12"> <form class="form-horizontal" role="form" action="/action/up_save" method="post"> @defining(actorForm("id")){ idField => <input type="hidden" id="@idField.id" name="@idField.name" value="@actor.id"> } <!--name--> <div class="form-group"> @defining(playerForm("name")){ nameField => player.nameの内容⇒名前は必須です。 <label for="nameField.id" class="col-sm-2 control-label">@Messages.get("player.name")</label> <div class="col-sm-6"> <input type="text" id="@nameField.id" class="form-control" name="@nameField.name" value="@actor.name"> @if(nameField.hasErrors){ ↓↓↓↓↓↓↓↓ここにエラー表示が来ない <span class="help-block" style="color: #F00;">@nameField.errors.mkString(",")</span> } </div> } </div> </div> </div>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/26 23:21
2019/02/27 03:19