0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

サーバー側でバリデーションメッセージを解決する

Posted at

既存の(比較的簡易な)登録機能があって、一括登録機能を追加開発したい。
エラーはファイルとして返却する。
既存のBeanValidationを流用したい。

と、なったときに困ったのがエラーメッセージ。
Springでは適当にリクエストを受け付けるメソッドのパラメーターとしてBindingResultを設定したり、@ValidateをつけたFormを用意しておけばよしなにバリデーションをかけられるし、メッセージはview側で解決できる。
JSPでは(確か)<form:error>で表示できる。

が、今回はサーバー側でメッセージを解決せねばならない・・・。
幸い、MessageSourceResolvableを実装しているようなので、

@Autowired ms: MessageSource
@Autowired validator: Validator

/* formに値をつめるとして */
val e = new BeanPropertyBindingResult(form, "form")
ValidationUtils.invokeValidator(validator, form, e)
val msges = e.getFieldErrors.asScala.toSeq.map(e => ms.getMessage(e, Locale.JAPAN)

みたいな感じで解決できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?