Validate that 2 form fields are identical?

I am trying to validate that 2 password fields are identical during user registration. I found the crate Validator, with "custom", but I can't seem to find a way to pass the 2 struct value required.

The struct is:

struct RegisterTemplate {
    error: String,
    current_username: String,
    #[validate(length(min=8), custom= "validate_password_equal")]
    current_password: String,
    current_confirmation: String,
    current_name: String,
    current_imageurl: String,
    current_profile: String,
}

Is there a way to also pass the current_confirmation value to the validation function?

There's a must_match attribute that seems like just what you need:

    #[validate(must_match = "current_confirmation")]
    current_password: String,

I REALLY need to learn to read and search API docs properly.(I'm not a dev by trade) I had seen the "must_match" on the main page but never went for the description since I was sure it meant something like "value must match this", and not "must_match the other field.

If I had took the time to find and read this from the doc:

Validates that the 2 given fields match. Both fields are optionals

In the meantime, I decided to wrote all the validation by hand(works! But it took time...) I'm going to retry with must_match to see.

Thanks for the pointer!

Yeah, because we devs always read the docs thoroughly! :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.