3

For all my form submissions, I am creating documents to take in the submitted information

e.g. public static void formAction(@Valid FormDocument formDocument){ ... }

I like this as it keeps my controllers looking tidy and makes it easier to see what is being requested in a form.

My registration document looks (trimmed) like this:

@Email
@Required
public String email;
@Required
public String password;
@Required
public String confirmPassword;

My question is is there a way I can check that the password matches the confirmPassword field within this class itself. Currently I am checking in the controller and passing a validation message back in if it fails. I think it would be neater if it was done inside the document itself.

Thanks for any feedback/answers in advance!

  • possible duplicate of [Cross field validation with Hibernate Validator (JSR 303)](http://stackoverflow.com/questions/1972933/cross-field-validation-with-hibernate-validator-jsr-303) – axtavt Jan 10 '12 at 16:02

1 Answers1

8

From the Play documentation, they have your exact use case.

Equals

Checks that the value is equal to another parameter’s value, using the value’s equals method, e.g. for checking for a password confirmation field.

Annotation syntax:

@Equals("passwordConfirmation") String password

So, all you would need is

@Email
@Required
public String email;
@Equals("confirmPassword") 
public String password;
@Required
public String confirmPassword;
Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • @Codemwnci Do you know How I do this in scala? – SKK Jun 04 '16 at 05:49
  • 1
    @karthi read this https://www.playframework.com/documentation/2.5.x/ScalaForms , it shows how to create a form, set constraints and then validate it, It is very different to Play 1 (I assume when you say scala you mean play 2), but it is all there. – Codemwnci Jun 04 '16 at 09:10