views:

49

answers:

2

I am doing an user crud in spring-mvc.

My model has the following properties:

private Long id;
private String password;
private String username;
private Collection<Authority> myAuthorities;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;

I solved how to show the Authority class in this question.

Now I am willing my form to be able to have a second password field to confirm that the user typed the password correctly.

I don't want to add a confirmPassword property to the model, so my question is how to fix this the best way possible.

Edit:

Everything is working with axtavt's answer but I am missing a way to validate. I have the following method in my controller, but even though I place a @Validate ApplicationUserFormValidator isn't called.

    @RequestMapping(method = RequestMethod.POST)
 public ModelAndView create(Model model,
   @Valid @ModelAttribute ApplicationUserForm applicationUserFrom,
   BindingResult result) {
  ModelAndView modelAndView = new ModelAndView();

  if (result.hasErrors()) {
   modelAndView.setViewName(USER_CREATE_FORM);
  } else {
   modelAndView.setViewName(REDIRECT_TO_USER_LIST);
   modelAndView.addObject(USER_FORM_MESSAGE, USER_FORM_ADD_SUCCESSFUL);
   applicationUserService.save(applicationUserFrom.getUser);
  }

  return modelAndView;
 }
A: 

The way that I dealt with this was to put password confirmation and "are you sure you want to do this?" confirmation into the the access policy object that implemented fine-grained access control rules in the controller.

Rather than implementing user management all from scratch, you could use an off-the-shelf solution; e.g. Emmet. (Disclaimer - I'm the author.)

Stephen C
I guess I didn't explain myself correctly.I need my form to have a second field for the password. Something like:Password:Confirm password:And then use a validator to check that passwords are equal.
Macarse
+1  A: 

You may create an enclosing object to keep a confirmation:

public class ApplicationUserForm {
    private ApplicationUser user;
    private String confirmPassword;

    ...
}

-

Password: <form:password path = "user.password" />
Confirm password: <form:password path = "confirmPassword" />

Validator also works fine:

public class ApplicationUserFormValidator implements Validator {
    public void validate(Object target, Errors errors) {
        ApplicationUserForm f = (ApplicationUserForm) target;

        if (!f.getConfirmPassword().equals(f.getUser().getPassword())) ...

        errors.pushNestedPath("user");
        new ApplicationUserValidator().validate(f.getUser(), errors);
        errors.popNestedPath();
    }
    ...
}

EDIT: If you use @Valid annotation, you need to register a validator using @InitBinder or in the config as described in the docs. You may also use a fully declarative JSR-303-style validation, but i'm not sure how will it play with business constraints such as user.password == confirmPassword.

@InitBinder
public void initBinder(WebDataBinder b) {
    b.setValidator(new ApplicationUserFormValidator());
}
axtavt
@axtavt: The first part went great, but I just edit my question adding an issue with validation.Can you help me out with this?
Macarse
@Macarse: Edited.
axtavt
@axtavt: Cool, I found that in the docs while looking for the solution, but if I do that I lose the JSR-303 annotations in my model.For instance, my model has: @NotNull @Size(min = 6, max = 12) private String username;
Macarse
@Macarse: Then you need a JSR-303's `ConstraintValidator` instead of Spring's `Validator`: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html#validation-beanvalidation-spring-constraints
axtavt