Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:59:26+00:00 2026-06-10T04:59:26+00:00

I’m trying to manually add an individual email error message to my model but

  • 0

I’m trying to manually add an individual email error message to my model but nothing is displaying in the view.
I think it may be how I’m creating or attached the ObjectError to the BindingResult.
I’m adding the error inside the catch.

Here is the contents of result.errors when I leave email field empty and JSR-303 annotations kick in (error displays in view):

[Field error in object 'user' on field 'email': rejected value []; codes [NotEmpty.user.email,NotEmpty.email,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.email,email]; arguments []; default message [email]]; default message [may not be empty]]

Here is the contents of of result.errors after I manually add the ErrorObject(email error does not display in view):

[Error in object 'email': codes []; arguments []; default message [An account already exists for this email.]]

Controller:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
    public ModelAndView post(@Valid User user, BindingResult result)
    {

        if (result.hasErrors())
        {
            ModelAndView modelAndView = new ModelAndView(
                    Consts.MODEL_RESISTER_PAGE);
            modelAndView.addObject("user", user);
            return modelAndView;
        }
        else
        {
            try
            {
                userService.addUser(user);

                ModelAndView modelAndView = new ModelAndView(
                        Consts.MODEL_CARD_REPORTS_HOME_PAGE);
                modelAndView.addObject("userRegisteredSuccess", Boolean.TRUE);

                return modelAndView;
            }
            catch (DataIntegrityViolationException ex)
            {
                ObjectError error = new ObjectError("email","An account already exists for this email.");

                result.addError(error);

                ModelAndView modelAndView = new ModelAndView(
                        Consts.MODEL_RESISTER_PAGE);

                modelAndView.addAllObjects(result.getModel());
                modelAndView.addObject("user", user);

                return modelAndView;
            }
        }
    }

My Model:

@Entity
public class User implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = -5232533507244034448L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotEmpty
    @Size(min=2, max=15)
    private String firstname;

    @NotEmpty
    @Size(min=2, max=15)
    private String surname;

    @NotEmpty
    @Email
    private String email;

    @NotEmpty
    @Size(min=6, max=10)
    private String password;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getFirstname()
    {
        return firstname;
    }

    public void setFirstname(String firstname)
    {
        this.firstname = firstname;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setSurname(String surname)
    {
        this.surname = surname;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }
}

File add.html

<form id="registrationForm" action="#" th:action="@{/registration}" th:object="${user}" method="post" class="clearfix">
    <legend>Registration</legend>

    <div class="control-group input" th:class="${#fields.hasErrors('firstname')}? 'control-group input error'">
      <input type="text" th:field="*{firstname}" placeholder="Firstname" />
      <span class="help-block" th:if="${#fields.hasErrors('firstname')}" th:errors="*{firstname}"></span>
    </div>

    <div class="control-group input" th:class="${#fields.hasErrors('surname')}? 'control-group input error'">
      <input type="text" th:field="*{surname}" placeholder="Surname" />
      <span class="help-block" th:if="${#fields.hasErrors('surname')}" th:errors="*{surname}"></span>
    </div>

    <div class="control-group input" th:class="${#fields.hasErrors('email')}? 'control-group input error'">
      <input type="text" th:field="*{email}" placeholder="Email" />
      <span class="help-block" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></span>
    </div>

    <div class="control-group input" th:class="${#fields.hasErrors('password')}? 'control-group input error'">
        <input type="password" th:field="*{password}" placeholder="Password" />
        <span class="help-block" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>
    </div>

    <div class="clearfix">
      <input type="submit" class="btn btn-success btn-large" value="Register" />
    </div>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:59:28+00:00Added an answer on June 10, 2026 at 4:59 am

    I usually call result.rejectValue("property", "error.object"); to add errors to BindingResult. If you want to add a global object error, you could use result.reject("error.object");.

    So, in your code, instead of:

    ObjectError error = new ObjectError("email","An account already exists for this email.");
    result.addError(error);
    

    try with:

    result.rejectValue("email", "error.user", "An account already exists for this email.");
    

    Check the reference here.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.