Esempio n. 1
0
 public static void AddErrorMessage(this Page page, IDataErrorInfo obj, string validationGroup = null)
 {
     obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
         .Where(p => !String.IsNullOrWhiteSpace(obj[p.Name]))
         .Select(p => p.Name)
         .ToList()
         .ForEach(propertyName => AddErrorMessage(page, obj[propertyName], validationGroup));
 }
Esempio n. 2
0
 /// <summary>
 /// Calls AddErrorMessage for all errormessages contained in an IDataErrorInfo-object
 /// </summary>
 /// <param name="obj">The object containing the error data.</param>
 /// <param name="validationGroup">The validationgroup to add the validators to.</param>
 public static void AddErrorMessage(this Page page, IDataErrorInfo obj, string validationGroup = null)
 {
     // Add all error-messages of the object to the validationsummary
     obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
         .Select(p => p.Name)
         .Where(n => !String.IsNullOrWhiteSpace(obj[n]))
         .ToList()
         .ForEach(n => AddErrorMessage(page, obj[n], validationGroup));
 }
        public static string GetDataValidationError(this IDataErrorInfo target)
        {
            var errors = target.GetType()
                         .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                         .Select(p => target[p.Name])
                         .Where(e => !String.IsNullOrWhiteSpace(e));

            return(String.Join("\r\n", errors));
        }
Esempio n. 4
0
    //AddErrorMessage - går igenom egenskaper, om felmeddelande finns läggs det till ValidationSummary
    private void AddErrorMessage(IDataErrorInfo obj)
    {
        //går igenom samtliga egenskaper och hämtar ut egenskap som ev. har felmeddelande:
        var propertyNames = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                            .Where(p => String.IsNullOrWhiteSpace(p.Name))
                            .Select(p => p.Name);

        foreach (var propertyName in propertyNames)
        {
            //överför felmeddelande till ValidatorCollection:
            AddErrorMessage(obj[propertyName]);
        }
    }
Esempio n. 5
0
 /// <summary>
 /// Validates object for the given property.
 /// </summary>
 /// <returns></returns>
 public static string GetError(IDataErrorInfo model, string columnName)
 {
     ValidationContext context = new ValidationContext(model, null, null) { MemberName = columnName };
     object val = model.GetType().GetProperty(columnName).GetValue(model, null);
     List<ValidationResult> results = new List<ValidationResult>();
     if (!Validator.TryValidateProperty(val, context, results))
     {
         string v = string.Empty;
         results.ForEach((r) => v += r.ErrorMessage);
         return v;
     }
     return string.Empty;
 }
Esempio n. 6
0
        public static string GetError(this IDataErrorInfo obj)
        {
            var type            = obj.GetType();
            var propertyGetters = GetPropertyGetters(type);
            var errors          = propertyGetters
                                  .Keys.Select(k => GetError(obj, k))
                                  .Where(e => !string.IsNullOrEmpty(e));
            var validators = GetValidators(type);

            if (validators.ContainsKey(string.Empty))
            {
                errors = errors.Concat(
                    GetErrorMessages(validators[string.Empty], obj, new ValidationContext(obj, null, null)));
            }
            return(string.Join(Environment.NewLine, errors));
        }
        public static DataError[] Validate(this IDataErrorInfo model)
        {
            if (model != null)
            {
                var errorList = new List <DataError>();
                foreach (var prop in model.GetType().GetProperties())
                {
                    var error = model[prop.Name];
                    if (!string.IsNullOrEmpty(error))
                    {
                        errorList.Add(new DataError(prop.Name, error));
                    }
                }

                return(errorList.Count > 0 ? errorList.ToArray() : null);
            }

            return(null);
        }
Esempio n. 8
0
        public static string GetError(this IDataErrorInfo obj, string propertyName)
        {
            var type            = obj.GetType();
            var propertyGetters = GetPropertyGetters(type);
            var validators      = GetValidators(type);

            if (validators.ContainsKey(propertyName) && propertyGetters.ContainsKey(propertyName))
            {
                var context = new ValidationContext(obj, null, null)
                {
                    MemberName  = propertyName,
                    DisplayName = ReflectionHelper.GetDisplayName(obj, propertyName)
                };
                var propertyValue = propertyGetters[propertyName](obj);
                var errorMessages = GetErrorMessages(validators[propertyName], propertyValue, context);
                return(string.Join(Environment.NewLine, errorMessages));
            }

            return(string.Empty);
        }
Esempio n. 9
0
 private void AddErrorMessage(IDataErrorInfo obj)
 {
     var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
     foreach (var property in properties)
     {
         if (!String.IsNullOrWhiteSpace(obj[property.Name]))
         {
             AddErrorMessage(obj[property.Name]);
         }
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Går igenom samtliga publika egenskaper för obj och undersöker om felmeddelande finns som i så
        /// fall läggs till samlingen ValidatorCollection.
        /// </summary>
        /// <param name="obj">Referens till affärslogikobjekt.</param>
        private void AddErrorMessage(IDataErrorInfo obj)
        {
            // Hämtar och loopar igenom samtliga publika, icke statiska, egenskaper objektet har där det
            // finns det ett felmeddelande associerat med egenskapens namn.
            var propertyNames = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => !String.IsNullOrWhiteSpace(obj[p.Name]))
                .Select(p => p.Name);

            foreach (var propertyName in propertyNames)
            {
                // Överför meddelandet till samlingen ValidatorCollection.
                AddErrorMessage(obj[propertyName]);
            }
        }
Esempio n. 11
0
 public static bool HasErrors(this IDataErrorInfo info)
 {
     return(info.GetType().GetProperties().Any(p => info[p.Name] != null));
 }
Esempio n. 12
0
        /// <summary>
        /// Validate is called when Data binding is updating
        /// </summary>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            // This rule is called during the CommittedValue step, so the value is the
            // owner of the rule collection - either a BindingGroup or an individual
            // binding expression.
            BindingGroup      bindingGroup;
            BindingExpression bindingExpr;

            if ((bindingGroup = value as BindingGroup) != null)
            {
                // in a BindingGroup, check the item-level IDataErrorInfo for each
                // source item in the group
                IList items = bindingGroup.Items;
                for (int i = items.Count - 1; i >= 0; --i)
                {
                    IDataErrorInfo idei = items[i] as IDataErrorInfo;
                    if (idei != null)
                    {
                        string error = idei.Error;
                        if (!String.IsNullOrEmpty(error))
                        {
                            return(new ValidationResult(false, error));
                        }
                    }
                }
            }
            else if ((bindingExpr = value as BindingExpression) != null)
            {
                // in a binding, check the error info for the binding's source
                // property
                IDataErrorInfo idei = bindingExpr.SourceItem as IDataErrorInfo;
                string         name = (idei != null) ? bindingExpr.SourcePropertyName : null;

                if (!String.IsNullOrEmpty(name))
                {
                    // get the data error information, if any, by calling idie[name].
                    // We do this in a paranoid way, even though indexers with
                    // string-valued arguments are not supposed to throw exceptions.

                    // PreSharp uses message numbers that the C# compiler doesn't know about.
                    // Disable the C# complaints, per the PreSharp documentation.
                    #pragma warning disable 1634, 1691

                    // PreSharp complains about catching NullReference (and other) exceptions.
                    // It doesn't recognize that IsCritical[Application]Exception() handles these correctly.
                    #pragma warning disable 56500

                    string error;
                    try
                    {
                        error = idei[name];
                    }
                    catch (Exception ex)
                    {
                        if (CriticalExceptions.IsCriticalApplicationException(ex))
                        {
                            throw;
                        }

                        error = null;

                        if (TraceData.IsEnabled)
                        {
                            TraceData.TraceAndNotify(TraceEventType.Error,
                                                     TraceData.DataErrorInfoFailed(
                                                         name,
                                                         idei.GetType().FullName,
                                                         ex.GetType().FullName,
                                                         ex.Message),
                                                     bindingExpr);
                        }
                    }
                    #pragma warning restore 56500
                    #pragma warning restore 1634, 1691

                    if (!String.IsNullOrEmpty(error))
                    {
                        return(new ValidationResult(false, error));
                    }
                }
            }
            else
            {
                throw new InvalidOperationException(SR.Get(SRID.ValidationRule_UnexpectedValue, this, value));
            }

            return(ValidationResult.ValidResult);
        }
Esempio n. 13
0
        /// <summary>Performs validation checks on a value.</summary>
        /// <param name="value">The value to check.</param>
        /// <param name="cultureInfo">The culture to use in this rule.</param>
        /// <returns>The result of the validation.</returns>
        // Token: 0x060044E3 RID: 17635 RVA: 0x001385D4 File Offset: 0x001367D4
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            BindingGroup bindingGroup;

            if ((bindingGroup = (value as BindingGroup)) != null)
            {
                IList items = bindingGroup.Items;
                for (int i = items.Count - 1; i >= 0; i--)
                {
                    IDataErrorInfo dataErrorInfo = items[i] as IDataErrorInfo;
                    if (dataErrorInfo != null)
                    {
                        string error = dataErrorInfo.Error;
                        if (!string.IsNullOrEmpty(error))
                        {
                            return(new ValidationResult(false, error));
                        }
                    }
                }
            }
            else
            {
                BindingExpression bindingExpression;
                if ((bindingExpression = (value as BindingExpression)) == null)
                {
                    throw new InvalidOperationException(SR.Get("ValidationRule_UnexpectedValue", new object[]
                    {
                        this,
                        value
                    }));
                }
                IDataErrorInfo dataErrorInfo2 = bindingExpression.SourceItem as IDataErrorInfo;
                string         text           = (dataErrorInfo2 != null) ? bindingExpression.SourcePropertyName : null;
                if (!string.IsNullOrEmpty(text))
                {
                    string text2;
                    try
                    {
                        text2 = dataErrorInfo2[text];
                    }
                    catch (Exception ex)
                    {
                        if (CriticalExceptions.IsCriticalApplicationException(ex))
                        {
                            throw;
                        }
                        text2 = null;
                        if (TraceData.IsEnabled)
                        {
                            TraceData.Trace(TraceEventType.Error, TraceData.DataErrorInfoFailed(new object[]
                            {
                                text,
                                dataErrorInfo2.GetType().FullName,
                                ex.GetType().FullName,
                                ex.Message
                            }), bindingExpression);
                        }
                    }
                    if (!string.IsNullOrEmpty(text2))
                    {
                        return(new ValidationResult(false, text2));
                    }
                }
            }
            return(ValidationResult.ValidResult);
        }
 public static bool IsDataValid(this IDataErrorInfo target)
 {
     return(target.GetType()
            .GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .All(p => String.IsNullOrWhiteSpace(target[p.Name])));
 }