Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 251 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Customize spring validation error

#1
I want customize the spring validation error for

@NotNull
@Length(max = 80)
private String email;

but I'm unable to do it.
What are the step to follow?
Reply

#2
The [JSR 303 default message interpolation algorithm][1] allows you to customize messages by supplying a resource bundle named ValidationMessages. Create a `ValidationMessages.properties` file in the classpath containing:

javax.validation.constraints.NotNull.message=CUSTOM NOT NULL MESSAGE
javax.validation.constraints.Size.message=CUSTOM SIZE MESSAGE

This changes the default message for the `@Size` constraint, so you should use the `@Size` constraint instead of the Hibernate-specific `@Length` constraint.

Instead of changing the default message for all constraints, you can change the message for a specific constraint instance. Set the `message` attribute on the constraint:

@NotNull(message = "{email.notnull}")
private String email;

And add the message to the `ValidationMessages.properties` file:

email.notnull=E-mail address is required


[1]:

[To see links please register here]

Reply

#3
By Spring I am assuming you mean Spring MVC.

From the below reference

[To see links please register here]


Here you go -

You create a validator class -

public class UserValidator implements Validator {

public boolean supports(Class candidate) {
return User.class.isAssignableFrom(candidate);
}

public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
}
}

Put in any validation text you want in the above field.

In the JSP you will need the following tag -

<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
<!-- Show errors for firstName field -->
<td><form:errors path="firstName" /></td>
</tr>

This way any validation error for `firstName` will be printed.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through