Play framework Scala validation

308 Views Asked by At

I am trying to implement password change.

I got the following two lines in scala. However even if I enter different passwords in both fields(Password & Confirm Password), it is saving the 1st field password WITHOUT validating.

I need it to get validated and display message if different passwords have been entered.

@helper.inputPassword(accountForm("Password"),'id -> "f_password", '_label -> "Password", 'placeholder -> "**********")
@helper.inputPassword(accountForm("Password"),'id -> "f_password", '_label -> "Confirm Password", 'placeholder -> "**********")

I have googled but can't seem to find the exact solution. Any help is much appreciated thanks.

UPDATE

I have got the following method in controllers in NewPasswordController.java

public class NewPasswordController extends Controller {
    public static class NewPassword {
    @Required
    protected String newPassword = "";
    @Required
    protected String confirmPassword = "";

    public String getNewPassword() { return this.newPassword; }

    public void setNewPassword(String newPassword) { this.newPassword = newPassword; }

    public String getConfirmPassword() { return this.confirmPassword; }

    public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; }

    public String validate() {
        if(!newPassword.equals(confirmPassword)) {
            return "Opps, it seems that you may have mis-typed the password, please try again.";
        }
        return null;
    }
}

But I wasn't sure how to use this in my SCALA.

I have tried like following

@helper.form(action = routes.NewPasswordController.NewPassword()) {

BUT this is not working, as my routes file doesn't have it. So, I am not sure how to mention NewPasswordController in routes (I mean is it get or set).

1

There are 1 best solutions below

4
On

don't know about that way you are using is actually works or not, but I think you need two inputs for passwords

@helper.inputPassword(accountForm("password"),'id -> "f_password", '_label -> "Password", 'placeholder -> "**********")
@helper.inputPassword(accountForm("confirmPassword"),'id -> "r_password", '_label -> "Confirm Password", 'placeholder -> "**********")

you need something like this in Your accountForm

public String confirm() {
    if (passwod!=confrimPassword) {
        return "password did not match";
    }
    return null;
}//offcourse you can convert into scala