Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare validator is always firing off even when the passwords are the same

Here's the reason why. This is the generated HTML:

    <div>
        <label for="RegisterModel_Password">Contrase&#241;a</label>
        <input class="text-box single-line password" data-val="true" data-val-length="Su Contrase&amp;#241;a debe tener al menos 6 caracteres." data-val-length-max="100" data-val-length-min="6" data-val-required="Debe escribir su contrase&amp;#241;a" id="RegisterModel_Password" name="RegisterModel.Password" type="password" value="" />
        <span class="field-validation-valid" data-valmsg-for="RegisterModel.Password" data-valmsg-replace="true"></span>

    </div>

    <div>
        <label for="RegisterModel_ConfirmPassword">Confirme Su Contrase&#241;a</label>
        <input class="text-box single-line password" data-val="true" data-val-equalto="Sus contrase&amp;#241;as no son las mismas." data-val-equalto-other="*.Password" id="RegisterModel_ConfirmPassword" name="RegisterModel.ConfirmPassword" type="password" value="" />
        <span class="field-validation-valid" data-valmsg-for="RegisterModel.ConfirmPassword" data-valmsg-replace="true"></span>
    </div>

Notice this, in the confirm password box:

data-val-equalto-other="*.Password"

This should be RegisterModel.Password, as I'm guessing the javascript looks like the input with the name "RegisterModel.Password", no?

Here's my model code:

[Required(ErrorMessage = "Debe escribir su contraseña")]
[StringLength(100, ErrorMessage = "Su {0} debe tener al menos {2} caracteres.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Contraseña")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirme Su Contraseña")]
[Compare("Password", ErrorMessage = "Sus contraseñas no son las mismas.")]
public string ConfirmPassword { get; set; }

Any ideas on why this is happening?


Just created a brand new MVC3 application and this is the model:

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

And the validation works. The only difference here is that they're use the model directly in the view, whereas I'm passing a *View*Model that contains that RegisterModel.

The HTML of this working default is different to mine though:

        <div class="editor-field">
            <input data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="100" data-val-length-min="6" data-val-required="The Password field is required." id="Password" name="Password" type="password" />
            <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label">
            <label for="ConfirmPassword">Confirm password</label>
        </div>
        <div class="editor-field">
            <input data-val="true" data-val-equalto="The password and confirmation password do not match." data-val-equalto-other="*.Password" id="ConfirmPassword" name="ConfirmPassword" type="password" />
            <span class="field-validation-valid" data-valmsg-for="ConfirmPassword" data-valmsg-replace="true"></span>
        </div>
like image 482
Only Bolivian Here Avatar asked Nov 05 '22 16:11

Only Bolivian Here


1 Answers

Hello I had the same problem than you. This happens because there is a bug on the javascript file that performs the validation. Here is the fix you have to make.

http://forums.asp.net/t/1716181.aspx/1

It is not that hard to find in the minimized version, which is what I had to change.

Saludos!

like image 51
paddingtonMike Avatar answered Nov 09 '22 05:11

paddingtonMike