1
Vote

Validate PasswordBox

description

Hello,
 
Thanks for such a great framework, Can you add support to PassworBox control, currently adding validator to PasswordBox doesn't work.
 
Thanks!

comments

jtoth55 wrote May 14, 2009 at 7:08 PM

If you download the source code and open up Silverlight.Validators/ValidatorBase.cs, this is where the validation happens and it checks what type of control it is. In my case I needed to validate required comboboxes and passwordboxes, so I went down to the ValidateRequired() method and changed it to the following:

protected bool ValidateRequired()
    {
        if (IsRequired)
        {
            if (ElementToValidate is TextBox)
            {
                TextBox box = ElementToValidate as TextBox;
                return !String.IsNullOrEmpty(box.Text);
            }
            if (ElementToValidate is PasswordBox)
            {
                PasswordBox box = ElementToValidate as PasswordBox;
                return !String.IsNullOrEmpty(box.Password);
            }
            if (ElementToValidate is ComboBox)
            {
                ComboBox box = ElementToValidate as ComboBox;
                return box.SelectionBoxItem != null;
            }
        }
        return true;
    }

jtoth55 wrote May 14, 2009 at 7:09 PM

If you download the source code and open up Silverlight.Validators/ValidatorBase.cs, this is where the validation happens and it checks what type of control it is. In my case I needed to validate required comboboxes and passwordboxes, so I went down to the ValidateRequired() method and changed it to the following:

protected bool ValidateRequired()
    {
        if (IsRequired)
        {
            if (ElementToValidate is TextBox)
            {
                TextBox box = ElementToValidate as TextBox;
                return !String.IsNullOrEmpty(box.Text);
            }
            if (ElementToValidate is PasswordBox)
            {
                PasswordBox box = ElementToValidate as PasswordBox;
                return !String.IsNullOrEmpty(box.Password);
            }
            if (ElementToValidate is ComboBox)
            {
                ComboBox box = ElementToValidate as ComboBox;
                return box.SelectionBoxItem != null;
            }
        }
        return true;
    }

jtoth55 wrote May 14, 2009 at 8:00 PM

Actually there are 3 places that need to be updated to support PasswordBox and ComboBox, if you do a search in the project for "is TextBox" then you'll see all 3 of them...

wrote Feb 13, 2013 at 6:57 PM