Example #1
0
        protected override bool EvaluateIsValid()
        {
            _HasBeenChecked = true;
            if (String.IsNullOrEmpty(SourceTypeString))
            {
                if (SourceType == null &&
                    String.IsNullOrEmpty(XmlRuleSetName) &&
                    this.Page as IWFGetValidationRulesForPage == null)
                {
                    throw new Exception("The SourceType and SourceTypeString properties are null/empty on one of the validator controls.\r\nPopulate either property.\r\nie: control.SourceType = typeof(Widget); OR in markup SourceTypeString=\"Assembly.Classes.Widget, Assembly\"\r\nFinally, the page can also implement IWFGetValidationRulesForPage.");
                }
                else if (SourceType == null &&
                         !String.IsNullOrEmpty(XmlRuleSetName))
                {
                    //Get the type from the XmlRuleSet
                    SourceType = WFUtilities.GetRuleSetForName(XmlRuleSetName).ModelType;
                }
                else if (SourceType == null && String.IsNullOrEmpty(XmlRuleSetName))
                {
                    SourceType = ((IWFGetValidationRulesForPage)this.Page).GetValidationClassType();
                }
            }
            else
            {
                try {
                    SourceType = Type.GetType(SourceTypeString, true, true);
                } catch (Exception ex) {
                    throw new Exception("Couldn't resolve type " + SourceTypeString + ". You may need to specify the fully qualified assembly name.");
                }
            }

            PropertyInfo prop = WFUtilities.GetTargetProperty(_propertyName, SourceType);

            Control validateControl = this.FindControl(this.ControlToValidate); //Search siblings

            if (validateControl == null)                                        //Search page
            {
                validateControl = WebControlUtilities.FindControlRecursive(this.Page, this.ControlToValidate);
            }
            string controlValue = WebControlUtilities.GetControlValue(validateControl);

            if (String.IsNullOrEmpty(XmlRuleSetName))
            {
                var    displayNameAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true).OfType <DisplayNameAttribute>().FirstOrDefault();
                string displayName     = displayNameAttr == null ? prop.Name : displayNameAttr.DisplayName;

                foreach (var attr in prop.GetCustomAttributes(typeof(ValidationAttribute), true).OfType <ValidationAttribute>())
                {
                    if (attr as IWFRequireValueProviderContext != null)
                    {
                        ((IWFRequireValueProviderContext)attr).SetValueProvider(new WFPageControlsValueProvider(this.Page, ""));
                    }

                    if (!attr.IsValid(controlValue))
                    {
                        this.ErrorMessage = attr.FormatErrorMessage(displayName);
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                XmlDataAnnotationsRuleSet         ruleset  = WFUtilities.GetRuleSetForType(SourceType, XmlRuleSetName);
                XmlDataAnnotationsRuleSetProperty property = ruleset.Properties.FirstOrDefault(p => p.PropertyName == PropertyName);
                if (property != null)
                {
                    foreach (XmlDataAnnotationsValidator val in property.Validators)
                    {
                        ValidationAttribute attr = WFUtilities.GetValidatorInstanceForXmlDataAnnotationsValidator(val);

                        if (attr as IWFRequireValueProviderContext != null)
                        {
                            ((IWFRequireValueProviderContext)attr).SetValueProvider(new WFPageControlsValueProvider(this.Page, ""));
                        }

                        if (!String.IsNullOrEmpty(ErrorMessage) &&
                            String.IsNullOrEmpty(attr.ErrorMessage))
                        {
                            attr.ErrorMessage = ErrorMessage;
                        }

                        foreach (var key in val.ValidatorAttributes.Keys)
                        {
                            PropertyInfo pi = attr.GetType().GetProperty(key);

                            string[] excludeProps = new string[] { };
                            if (attr.GetType() == typeof(StringLengthAttribute))
                            {
                                excludeProps = new string[] { "maximumLength" };
                            }
                            if (attr.GetType() == typeof(RangeAttribute))
                            {
                                excludeProps = new string[] { "minimum", "maximum" };
                            }
                            if (attr.GetType() == typeof(RegularExpressionAttribute))
                            {
                                excludeProps = new string[] { "pattern" };
                            }
                            if (!excludeProps.Contains(key))
                            {
                                pi.SetValue(attr, Convert.ChangeType(val.ValidatorAttributes[key], pi.PropertyType), null);
                            }
                        }
                        if (!attr.IsValid(controlValue))
                        {
                            this.ErrorMessage = attr.FormatErrorMessage(property.DisplayName ?? "");
                            return(false);
                        }
                    }
                }

                return(true);
            }
        }