I am building a feature for users to change their passwords. So this means I have three text fields that need to be validated (is current == actual current, and is new == repeat). So I'm trying to build a custom validator for the first time, and am not sure how to do this. From another example I've seen, the validator is tied to one input field only, which means the ControlToValidate property is rather self-explainatory. But now, though?
I'm trying to do this:
var passwordValidator = new CustomValidator()
{
Display = ValidatorDisplay.None,
ValidationGroup = "PasswordValidationGroup"
};
passwordValidator.ServerValidate += ChangePasswordServerValidate;
ValidationPlaceHolder.Controls.Add(passwordValidator);
But I'm not getting the results I'm after. The validator does get executed when expected, and debugging shows it follows the if/else-structure as expected, but the error message never does get set. So how do I set it to validate three controls? Is it related to ValidationGroup somehow? I just set that property similarly to how it was in that other custom validator.
private void ChangePasswordServerValidate(object sender, ServerValidateEventArgs e)
{
var validator = sender as IValidator;
var user = SessionManager.Get<OrderFacade>(SessionKeys.OrderFacade).User;
if (CurrentPasswordTextBox.Text == "" || NewPasswordTextBox.Text == "" || RepeatPasswordTextBox.Text == "")
{
validator.ErrorMessage = SiteTextResources.CreateAccount_YourEmailAddressEmpty;
e.IsValid = false;
}
else if (!NewPasswordTextBox.Text.Equals(RepeatPasswordTextBox.Text))
{
validator.ErrorMessage = SiteTextResources.CreateAccount_YourEmailAddressEmpty;
e.IsValid = false;
}
else
{
ResolveClient<IUserClient>().TryPassword(user.UserName, CurrentPasswordTextBox.Text, passwordSuccessfullyChanged =>
{
e.IsValid = passwordSuccessfullyChanged;
validator.ErrorMessage = String.Empty;
}, error =>
{
});
}
}
The error message doesn't get set because you are setting the display to None
. Change it to Static
or Dynamic
. From the documentation :
As for the validation details, if you need to check the contents of 3 controls, do it inside the ChangePasswordServerValidate
method and set the e.IsValid
accordingly. The ControlToValidate
property is not relevant here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.