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 3335908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:04:22+00:00 2026-05-18T00:04:22+00:00

I have a form to create a user and a corresponding command object. It

  • 0

I have a form to create a user and a corresponding command object. It looks like so:

public class CreateUserForm {
    private String name;
    private String email;
    private String password1;
    private String password2;
    private Boolean enabled = true;

    ... getters and setters ...
}

I have a user entity object that is to be created and stored based on the form input. The entity has annotated validation rules and looks like so:

@Entity
public class SiteUser {
    @Id
    private String id;

    @NotEmpty
    private String name;

    @NotEmpty
    @Email
    private String email;

    @NotEmpty
    @Pattern(regexp="^[a-zA-Z0-9_]{6,20}$")
    private String password;

    @NotEmpty
    private Boolean enabled;

    ... getters and setters ...
}

The form input should essentially have the same validation rules apart from the fact that the form input needs to check if the password1 and password2 fields are identical.

My first thought is to add the same validation annotations to the form object and create a custom validator that checks to see if the password1 and password2 fields are the same. However, I feel like I’m not doing something right by putting the same validation rules across two objects that are so similar.

So I’m looking for any advice on how to consolidate the process of validating and storing my entity based on the form input.

  • 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-05-18T00:04:22+00:00Added an answer on May 18, 2026 at 12:04 am

    You can do something like this:

    public class CreateUserForm { 
        @Valid
        private SiteUser user;
        private String confirmPassword;
        ...
    }
    

    and check that confirmPassword.equals(user.getPassword()) manually.

    Update:

    Manual validation looks like this:

    @RequestMapping(...)
    public String createUser(@Valid CreateUserForm form, BindingResult result) {
        if (!form.getConfirmPassword().equals(form.getUser().getPassword())) {
            result.rejectValue("confirmPassword", ...);
        }
        ...
    }
    

    You can also implement a custom JSR-303 constraint to validate password confirmation along with other fields, but I think it’s overkill in this case.

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

Sidebar

Related Questions

I have a form on a page that a user can use to create
I have a form. In that form I create an instance of a class
I have a web form that uses if statements to create a string of
I have a form to create events. If an event is repeating, the user
I'm developing a web app where i have a form like that <form name=form
I have a form where a user can create an account. The username column
I have an entity class User that contains information such as username, first name,
I have created a PHP form which requires the user to select a postcode
I have a form which I create manually (a lot of JavaScript in it...).
i have create a form (so it's PHP and HTML hybrid-code). it has ability

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.