0

My Spring MVC form validation is working, but how do I expand parameters or "tokens" in my .properties file?

E.g.: messages.properties

error.field.required=<b><a>{0}</a></b> is required

Spring MVC Validator class which adds the error/code:

@Component
public class MyModelValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return MyModel.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        MyModel myModel = (MyModel)target;

        ValidationUtils.rejectIfEmpty(errors, "address.addressLine1", "error.field.required");

    }

}

I need to pass in a custom string, such as "Address Line 1" to replace the {0} token with a description.

1 Answer 1

1

You can pass values for message placeholders using errorArgs parameter of the rejectIfEmpty method.

ValidationUtils.rejectIfEmpty(errors,
        "address.addressLine1",
        "error.field.required",
        new Object[] { "value for 1st placeholder", "value for 2nd placeholder" });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.