0

My thymeleaf code:

Your favorite number: <input type="text" th:field="*{xy}"/>

My messages.properties:

  required={0} is required
  typeMismatch.java.lang.Integer={0} is not a number

When the user doesn’t fill this field, or types bad data, this message has displayed automatically:

  xy is required 

or

  xy is not a number

But I would like to set the field name „Your favorite number” instead of „xy”, so I would like to display these messages when error:

  Your favorite number is required 

or

  Your favorite number is not a number

How can I set field name for {0}?

albert
  • 8,027
  • 10
  • 48
  • 84

1 Answers1

0

According to this question you can specify the message parameters after the message key. So you should be able to do something like:

<div th:text="#{${error}('Your favorite number')}">Error message</div>

This will fill the {0} parameter with whatever you pass it. error is a variable containing the validation message (presumably from a call to hasErrors or similar).

You could even move your field name into your message properties, in which case you just need to nest another message:

<div th:text="#{${error}(#{field.xy})}">Error message</div>

And in message.properties:

field.xy=Your favorite number

This would then insert the contents of the field.xy message into the placeholder {0} of the validation message.

Community
  • 1
  • 1
Andrew
  • 1,269
  • 7
  • 9