ponder275
ponder275

Reputation: 935

How to pass a field name to the global message in the properties file

I have the following message in my global-messages.properties file.

errors.integer=${getText(fieldname)} must be an integer.

which works fine with the validation.xml code, but I want to be able use the same message in my java action validation method with the addFieldError() method. My question is how to pass the fieldname to the message. If I use:

addFieldError("seqId", getText("errors.integer"));

I only get the "must be an integer." part of the message. I know I could change the message and use {0} instead of ${getText(fieldname)} but that is not an option because other code uses the message as it is.

Upvotes: 3

Views: 417

Answers (2)

ponder275
ponder275

Reputation: 935

Someone else showed me another way which worked which I thought I would share.

addFieldError("",getText("seqId")+ getText("errors.integer"));

Upvotes: 2

Aleksandr M
Aleksandr M

Reputation: 24396

First of all: You should really avoid using getText in properties because it is available only in some context.

Second: You should really avoid using fieldname in properties because it is validator specific field.

To achieve what you want, w/o modifying property file, you can create a fieldname property in your action with getter/setter and set its value before using addFieldError.

private String fieldname;
// getter/setter

// ...
fieldname = "seqId";
addFieldError("seqId", getText("errors.integer"));

Upvotes: 3

Related Questions