Mike Cole
Mike Cole

Reputation: 14733

Checking if a ValidationGroup is valid from code-behind

Is there a method I can call that retrieves a boolean value of whether or not a particular ValidationGroup is valid? I don't want to actually display the validation message or summary - I just want to know whether it is valid or not.

Something like:

Page.IsValid("MyValidationGroup")

Upvotes: 29

Views: 41396

Answers (6)

Rory
Rory

Reputation: 11

Pavel's answer works but isn't the simplest. Here is how I solved it:

protected Boolean validateGroup(String validationGroupName) {
        Boolean isGroupValid = true;

        foreach (BaseValidator validatorControl in Page.GetValidators(validationGroupName)) {
            validatorControl.Validate();

            if (!validatorControl.IsValid)
                isGroupValid = false;
        }

        if (!isGroupValid)
            return false;
        else
            return true;
    }

Upvotes: 1

Ahmad Ahmadi
Ahmad Ahmadi

Reputation: 367

var isValidGroup = Page
                    .GetValidators(sValidationGroup)
                    .Cast<IValidator>()
                    .All(x => x.IsValid);

Upvotes: 10

Tim Witham
Tim Witham

Reputation: 31

Page.IsValid will be false if any of the validated validation groups was invalid. If you want to validate a group and see the status, try:

    protected bool IsGroupValid(string sValidationGroup)
    {
        Page.Validate(sValidationGroup);
        foreach (BaseValidator validator in Page.GetValidators(sValidationGroup))
        {
            if (!validator.IsValid)
            {
                return false;
            }
        }
        return true;
    }

Upvotes: 3

Pavel Chuchuva
Pavel Chuchuva

Reputation: 22465

protected bool IsGroupValid(string sValidationGroup)
{
   foreach (BaseValidator validator in Page.Validators)
   {
      if (validator.ValidationGroup == sValidationGroup)
      {
         bool fValid = validator.IsValid;
         if (fValid)
         {
            validator.Validate();
            fValid = validator.IsValid;
            validator.IsValid = true;
         }
         if (!fValid)
            return false;
      }

   }
   return true;
}

Upvotes: 17

Scott Ivey
Scott Ivey

Reputation: 41578

Have you tried using the Page.Validate(string) method? Based on the documentation, it looks like it may be what you want.

Page.Validate("MyValidationGroup");
if (Page.IsValid)
{
    // your code here.
}

Note that the validators on the control that also caused the postback will also fire. Snip from the MSDN article...

The Validate method validates the specified validation group. After calling the Validate method on a validation group, the IsValid method will return true only if both the specified validation group and the validation group of the control that caused the page to be posted to the server are valid.

Upvotes: 38

Matthew Jones
Matthew Jones

Reputation: 26190

Try this:

Page.Validate("MyValidationGroup");
if (Page.IsValid) 
{
    //Continue with your logic
}
else
{
    //Display errors, hide controls, etc.
}

Not exactly what you want, but hopefully close.

Upvotes: 4

Related Questions