Ejemplo n.º 1
0
        internal IReadOnlyList <string> FetchErrors(string?property)
        {
            var errors = new List <string>();

            if (property is null)
            {
                return(errors);
            }
            var exist = ValidationAttributes.TryGetValue(property, out var attr);

            if (!exist || attr is null)
            {
                return(errors);
            }

            var value = GetType().GetProperty(property)?.GetValue(this);

            foreach (var validator in attr)
            {
                var res = validator.ErrorGenerator(value);
                if (string.IsNullOrWhiteSpace(res))
                {
                    continue;
                }

                errors.Add(res);
            }

            return(errors);
        }
Ejemplo n.º 2
0
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (RenderUplevel)
            {
                this.RegisterExpandoAttribute(writer, "evaluationfunction", "DAValidation.DataAnnotationsValidatorIsValid");

                var validationRules = ValidationAttributes.SelectMany(attribute => ClientValidationRulesProvider.GetClientValidationRules(attribute, DisplayName));
                var errorMessages   = ValidationAttributes.Select(attribute => attribute.FormatErrorMessage(DisplayName));

                var validatorFunctions = new List <string>();
                foreach (var rule in validationRules)
                {
                    validatorFunctions.Add(rule.EvaluationFunction);
                    foreach (var ruleParameter in rule.Parameters)
                    {
                        this.RegisterExpandoAttribute(writer, ruleParameter.Key, Convert.ToString(ruleParameter.Value, CultureInfo.InvariantCulture));
                    }
                }

                this.RegisterExpandoAttribute(writer, "validatorfunctions", string.Join(";;", validatorFunctions));
                this.RegisterExpandoAttribute(writer, "errormessages", string.Join(";;", errorMessages));
                this.RegisterExpandoAttribute(writer, "supresserrormessagetext", Text.Trim().Length > 0 ? "true" : "false");
            }

            base.AddAttributesToRender(writer);
        }
Ejemplo n.º 3
0
        protected override IEnumerable <ValidationAttribute> GetAttributes(MemberInfo propertyInfo)
        {
            var returnValue = base.GetAttributes(propertyInfo);

            if (ValidationAttributes.HasAttributes(propertyInfo.Name))
            {
                returnValue = returnValue.Concat(ValidationAttributes[propertyInfo.Name]);
            }

            return(returnValue);
        }
        private async Task <bool> RunValidCheckOnName(object target, CommerceContext commerceContext)
        {
            ValidationPolicy     validationPolicy     = new ValidationPolicy();
            List <Model>         models               = validationPolicy.Models;
            ValidationAttributes validationAttributes = new ValidationAttributes();

            validationAttributes.Name                    = "Name";
            validationAttributes.MaxLength               = 50;
            validationAttributes.RegexValidator          = "^[\\w\\s]*$";
            validationAttributes.RegexValidatorErrorCode = "AlphanumericOnly_NameValidationError";
            models.Add(validationAttributes);

            return(await validationPolicy.ValidateModels(target, commerceContext.PipelineContext).ConfigureAwait(false));
        }
Ejemplo n.º 5
0
        /// <summary>
        ///   Retrieves the member attributes.
        /// </summary>
        protected override void RetrieveMemberAttributes()
        {
            List <FieldInfo> fieldInfos = new List <FieldInfo>(MemberType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance));

            foreach (FieldInfo fieldInfo in fieldInfos)
            {
                List <Attribute> customAttributes = new List <Attribute>(Attribute.GetCustomAttributes(fieldInfo, typeof(ValidationAttribute), true));
                foreach (Attribute attribute in customAttributes)
                {
                    ValidationAttribute att = (ValidationAttribute)attribute;
                    att.Target = fieldInfo.GetValue(BuilderSource.Action);
                    ValidationAttributes.Add(att);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Register a custom <see cref="IValidator" />
        /// </summary>
        /// <param name="validator">The validator</param>
        protected void RegisterValidator(IValidator validator)
        {
            if (validator is null)
            {
                throw new ArgumentNullException(nameof(validator));
            }

            ValidationAttributes.AddOrUpdate(
                validator.PropertyName,
                new List <IValidator> {
                validator
            },
                (s, list) =>
            {
                list.Add(validator);
                return(list);
            });
        }
Ejemplo n.º 7
0
        private void InitializeDataValidator()
        {
            var props = GetType().GetProperties().SelectMany(p => p.GetCustomAttributes(typeof(IValidator), true).OfType <IValidator>());

            foreach (var validatorAttribute in props)
            {
                ValidationAttributes.AddOrUpdate(
                    validatorAttribute.PropertyName,
                    new List <IValidator> {
                    validatorAttribute
                },
                    (s, list) =>
                {
                    list.Add(validatorAttribute);
                    return(list);
                });
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///   Retrieves the member attributes.
        /// </summary>
        protected override void RetrieveMemberAttributes()
        {
            List <PropertyInfo> propertyInfos = new List <PropertyInfo>(MemberType.GetProperties(BindingFlags.Public | BindingFlags.Instance));

            foreach (PropertyInfo info in propertyInfos)
            {
                List <Attribute> propertyAttributes = new List <Attribute>(Attribute.GetCustomAttributes(info, typeof(ValidationAttribute), true));
                foreach (Attribute attribute in propertyAttributes)
                {
                    if (attribute is ValidationAttribute)
                    {
                        ValidationAttribute att = (ValidationAttribute)attribute;
                        att.Target = info.GetValue(BuilderSource.Action, null);
                        ValidationAttributes.Add(att);
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public ValidationAttribute GetAttribute(Type attributeType)
 {
     return(ValidationAttributes.FirstOrDefault(a => a.GetType() == attributeType));
 }
Ejemplo n.º 10
0
        protected virtual async Task PopulateDetails(EntityView view, Customer customer, bool isAddAction, bool isEditAction, CommercePipelineExecutionContext context)
        {
            if (view == null)
            {
                return;
            }
            ValidationPolicy         validationPolicy        = ValidationPolicy.GetValidationPolicy(context.CommerceContext, typeof(Customer));
            ValidationPolicy         detailsValidationPolicy = ValidationPolicy.GetValidationPolicy(context.CommerceContext, typeof(CustomerDetailsComponent));
            CustomerPropertiesPolicy propertiesPolicy        = context.GetPolicy <CustomerPropertiesPolicy>();
            EntityView details = (EntityView)null;

            if (customer != null && customer.HasComponent <CustomerDetailsComponent>())
            {
                details = customer.GetComponent <CustomerDetailsComponent>().View.ChildViews.FirstOrDefault <Model>((Func <Model, bool>)(v => v.Name.Equals("Details", StringComparison.OrdinalIgnoreCase))) as EntityView;
            }
            List <string> languages = new List <string>();
            Shop          shop      = context.CommerceContext.GetObjects <Shop>().FirstOrDefault <Shop>();

            if (shop != null && shop.Languages.Any <string>())
            {
                languages = shop.Languages;
            }
            foreach (string detailsProperty in propertiesPolicy?.DetailsProperties)
            {
                string propertyName = detailsProperty;
                if (!isAddAction || !propertyName.Equals(propertiesPolicy?.AccountNumber, StringComparison.OrdinalIgnoreCase))
                {
                    ValidationAttributes validationAttributes = validationPolicy.Models.FirstOrDefault <Model>((Func <Model, bool>)(m => m.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))) as ValidationAttributes;
                    if (propertyName.Equals(propertiesPolicy?.AccountStatus, StringComparison.OrdinalIgnoreCase))
                    {
                        KnownCustomersStatusesPolicy statusesPolicy = context.GetPolicy <KnownCustomersStatusesPolicy>();
                        List <Selection>             statuses       = new List <Selection>();
                        string currentStatus = customer?.AccountStatus ?? string.Empty;
                        if (isAddAction | isEditAction)
                        {
                            PropertyInfo[] propertyInfoArray = typeof(KnownCustomersStatusesPolicy).GetProperties();
                            for (int index = 0; index < propertyInfoArray.Length; ++index)
                            {
                                PropertyInfo propertyInfo = propertyInfoArray[index];
                                if (!propertyInfo.Name.Equals("PolicyId", StringComparison.OrdinalIgnoreCase) && !propertyInfo.Name.Equals("Models", StringComparison.OrdinalIgnoreCase))
                                {
                                    string status = propertyInfo.GetValue((object)statusesPolicy, (object[])null) as string;
                                    if (!string.IsNullOrEmpty(status))
                                    {
                                        LocalizedTerm localizedTerm = await this._getLocalizedCustomerStatusPipeline.Run(new LocalizedCustomerStatusArgument(status, (object[])null), context);

                                        List <Selection> selectionList = statuses;
                                        Selection        selection     = new Selection();
                                        selection.DisplayName = localizedTerm?.Value;
                                        selection.Name        = status;
                                        selectionList.Add(selection);
                                        status = (string)null;
                                    }
                                }
                            }
                            propertyInfoArray = (PropertyInfo[])null;
                        }
                        else if (!string.IsNullOrEmpty(currentStatus))
                        {
                            LocalizedTerm localizedTerm = await this._getLocalizedCustomerStatusPipeline.Run(new LocalizedCustomerStatusArgument(currentStatus, (object[])null), context);

                            if (!string.IsNullOrEmpty(localizedTerm?.Value))
                            {
                                currentStatus = localizedTerm?.Value;
                            }
                        }
                        List <ViewProperty> properties   = view.Properties;
                        ViewProperty        viewProperty = new ViewProperty();
                        viewProperty.Name       = propertiesPolicy?.AccountStatus;
                        viewProperty.RawValue   = (object)currentStatus;
                        viewProperty.IsReadOnly = !isAddAction && !isEditAction;
                        ValidationAttributes validationAttributes1 = validationAttributes;
                        viewProperty.IsRequired = validationAttributes1 != null && validationAttributes1.MinLength > 0;
                        viewProperty.Policies   = (IList <Policy>) new List <Policy>()
                        {
                            (Policy) new AvailableSelectionsPolicy()
                            {
                                List = statuses
                            }
                        };
                        properties.Add(viewProperty);
                    }
                    else if (propertyName.Equals(propertiesPolicy?.LoginName, StringComparison.OrdinalIgnoreCase))
                    {
                        List <ViewProperty> properties   = view.Properties;
                        ViewProperty        viewProperty = new ViewProperty();
                        viewProperty.Name       = propertiesPolicy?.LoginName;
                        viewProperty.RawValue   = (object)(customer?.LoginName ?? string.Empty);
                        viewProperty.IsReadOnly = !isAddAction;
                        viewProperty.IsRequired = true;
                        List <Policy> policyList;
                        if (isAddAction)
                        {
                            ValidationAttributes validationAttributes1 = validationAttributes;
                            if ((validationAttributes1 != null ? (validationAttributes1.MaxLength > 0 ? 1 : 0) : 0) != 0)
                            {
                                policyList = new List <Policy>()
                                {
                                    (Policy) new MaxLengthPolicy()
                                    {
                                        MaxLengthAllow = validationAttributes.MaxLength
                                    }
                                };
                                goto label_28;
                            }
                        }
                        policyList = new List <Policy>();
label_28:
                        viewProperty.Policies = (IList <Policy>)policyList;
                        properties.Add(viewProperty);
                    }
                    else if (propertyName.Equals(propertiesPolicy?.Domain, StringComparison.OrdinalIgnoreCase))
                    {
                        List <ViewProperty> properties   = view.Properties;
                        ViewProperty        viewProperty = new ViewProperty();
                        viewProperty.Name       = propertiesPolicy?.Domain;
                        viewProperty.RawValue   = (object)(customer?.Domain ?? string.Empty);
                        viewProperty.IsReadOnly = !isAddAction;
                        viewProperty.IsRequired = true;
                        List <Policy> policyList;
                        if (!isAddAction)
                        {
                            policyList = new List <Policy>();
                        }
                        else
                        {
                            policyList = new List <Policy>();
                            AvailableSelectionsPolicy selectionsPolicy = new AvailableSelectionsPolicy();
                            List <Selection>          selectionList;
                            if (propertiesPolicy?.Domains == null || !propertiesPolicy.Domains.Any <string>() || !(isAddAction | isEditAction))
                            {
                                selectionList = new List <Selection>();
                            }
                            else
                            {
                                CustomerPropertiesPolicy propertiesPolicy1 = propertiesPolicy;
                                selectionList = propertiesPolicy1 != null?propertiesPolicy1.Domains.Select <string, Selection>((Func <string, Selection>)(s =>
                                {
                                    return(new Selection()
                                    {
                                        DisplayName = s,
                                        Name = s
                                    });
                                })).ToList <Selection>() : (List <Selection>)null;
                            }
                            selectionsPolicy.List = selectionList;
                            policyList.Add((Policy)selectionsPolicy);
                        }
                        viewProperty.Policies = (IList <Policy>)policyList;
                        properties.Add(viewProperty);
                    }
                    else if (propertyName.Equals(propertiesPolicy?.UserName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (!isAddAction)
                        {
                            List <ViewProperty> properties   = view.Properties;
                            ViewProperty        viewProperty = new ViewProperty();
                            viewProperty.Name       = propertiesPolicy?.UserName;
                            viewProperty.RawValue   = (object)(customer?.UserName ?? string.Empty);
                            viewProperty.IsReadOnly = !isAddAction;
                            viewProperty.IsRequired = true;
                            List <Policy> policyList;
                            if (isAddAction)
                            {
                                ValidationAttributes validationAttributes1 = validationAttributes;
                                if ((validationAttributes1 != null ? (validationAttributes1.MaxLength > 0 ? 1 : 0) : 0) != 0)
                                {
                                    policyList = new List <Policy>()
                                    {
                                        (Policy) new MaxLengthPolicy()
                                        {
                                            MaxLengthAllow = validationAttributes.MaxLength
                                        }
                                    };
                                    goto label_43;
                                }
                            }
                            policyList = new List <Policy>();
label_43:
                            viewProperty.Policies = (IList <Policy>)policyList;
                            properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals(propertiesPolicy?.Language, StringComparison.OrdinalIgnoreCase))
                    {
                        object obj = details?.GetPropertyValue(propertiesPolicy?.Language) ?? (((languages == null ? 0 : (languages.Any <string>() ? 1 : 0)) & (isAddAction ? 1 : 0)) != 0 ? (object)languages.FirstOrDefault <string>() : (object)string.Empty);
                        List <ViewProperty> properties   = view.Properties;
                        ViewProperty        viewProperty = new ViewProperty();
                        viewProperty.Name       = propertiesPolicy?.Language;
                        viewProperty.RawValue   = obj ?? (object)string.Empty;
                        viewProperty.IsReadOnly = !isAddAction && !isEditAction;
                        ValidationAttributes validationAttributes1 = validationAttributes;
                        viewProperty.IsRequired = validationAttributes1 != null && validationAttributes1.MinLength > 0;
                        viewProperty.Policies   = (IList <Policy>) new List <Policy>()
                        {
                            (Policy) new AvailableSelectionsPolicy()
                            {
                                List = (languages == null || !languages.Any <string>() || !(isAddAction | isEditAction) ? new List <Selection>() : languages.Select <string, Selection>((Func <string, Selection>)(s =>
                                {
                                    return(new Selection()
                                    {
                                        DisplayName = s,
                                        Name = s
                                    });
                                })).ToList <Selection>())
                            }
                        };
                        properties.Add(viewProperty);
                    }
                    else if (propertyName.Equals("IsCompany", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, !isAddAction, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals("ConsentRegulation", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, false, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals("ConsentProcessingContactData", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, false, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals("CanPurchase", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, false, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals("CanCreateOrders", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, false, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else if (propertyName.Equals("CanSeeDiscountPrices", StringComparison.OrdinalIgnoreCase))
                    {
                        ViewProperty viewProperty = _registrationHelper.SetCustomViewProperty(details, propertyName, false, false.GetType(), true);
                        if (viewProperty != null)
                        {
                            view.Properties.Add(viewProperty);
                        }
                    }
                    else
                    {
                        PropertyInfo property = typeof(Customer).GetProperty(propertyName);
                        if (property != (PropertyInfo)null)
                        {
                            List <ViewProperty> properties   = view.Properties;
                            ViewProperty        viewProperty = new ViewProperty();
                            viewProperty.Name       = propertyName;
                            viewProperty.RawValue   = customer == null ? (object)string.Empty : property.GetValue((object)customer, (object[])null);
                            viewProperty.IsReadOnly = !isAddAction && !isEditAction || propertyName.Equals(propertiesPolicy?.AccountNumber, StringComparison.OrdinalIgnoreCase);
                            ValidationAttributes validationAttributes1 = validationAttributes;
                            viewProperty.IsRequired = validationAttributes1 != null && validationAttributes1.MinLength > 0;
                            List <Policy> policyList;
                            if (isAddAction | isEditAction)
                            {
                                ValidationAttributes validationAttributes2 = validationAttributes;
                                if ((validationAttributes2 != null ? (validationAttributes2.MaxLength > 0 ? 1 : 0) : 0) != 0)
                                {
                                    policyList = new List <Policy>()
                                    {
                                        (Policy) new MaxLengthPolicy()
                                        {
                                            MaxLengthAllow = validationAttributes.MaxLength
                                        }
                                    };
                                    goto label_51;
                                }
                            }
                            policyList = new List <Policy>();
label_51:
                            viewProperty.Policies = (IList <Policy>)policyList;
                            properties.Add(viewProperty);
                        }
                        else
                        {
                            object propertyValue = details?.GetPropertyValue(propertyName);
                            ValidationAttributes validationAttributes1 = detailsValidationPolicy.Models.FirstOrDefault <Model>((Func <Model, bool>)(m => m.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase))) as ValidationAttributes;
                            List <ViewProperty>  properties            = view.Properties;
                            ViewProperty         viewProperty          = new ViewProperty();
                            viewProperty.Name       = propertyName;
                            viewProperty.RawValue   = propertyValue ?? (object)string.Empty;
                            viewProperty.IsReadOnly = !isAddAction && !isEditAction;
                            viewProperty.IsRequired = validationAttributes1 != null && validationAttributes1.MinLength > 0;
                            List <Policy> policyList;
                            if (!(isAddAction | isEditAction) || validationAttributes1 == null || validationAttributes1.MaxLength <= 0)
                            {
                                policyList = new List <Policy>();
                            }
                            else
                            {
                                policyList = new List <Policy>()
                                {
                                    (Policy) new MaxLengthPolicy()
                                    {
                                        MaxLengthAllow = validationAttributes1.MaxLength
                                    }
                                }
                            };
                            viewProperty.Policies = (IList <Policy>)policyList;
                            properties.Add(viewProperty);
                            validationAttributes = (ValidationAttributes)null;
                        }
                    }
                }
            }
            List <string> stringList1 = new List <string>();

            if (customer?.Tags != null && customer.Tags.Any <Tag>())
            {
                List <string>        stringList2 = stringList1;
                Customer             customer1   = customer;
                IEnumerable <string> collection  = (customer1 != null ? customer1.Tags.Where <Tag>((Func <Tag, bool>)(t => !t.Excluded)) : (IEnumerable <Tag>)null).Select <Tag, string>((Func <Tag, string>)(tag => tag.Name));
                stringList2.AddRange(collection);
            }
            if (isAddAction)
            {
                return;
            }
            List <ViewProperty> properties1   = view.Properties;
            ViewProperty        viewProperty1 = new ViewProperty();

            viewProperty1.Name         = "IncludedTags";
            viewProperty1.RawValue     = (object)stringList1.ToArray();
            viewProperty1.IsReadOnly   = !isAddAction && !isEditAction;
            viewProperty1.IsRequired   = false;
            viewProperty1.Policies     = (IList <Policy>) new List <Policy>();
            viewProperty1.UiType       = isEditAction ? "Tags" : "List";
            viewProperty1.OriginalType = "List";
            properties1.Add(viewProperty1);
        }
    }
 public EmailAttribute()
 {
     ValidationAttributes.Add(new EmailAddressAttribute());
     ValidationAttributes.Add(new StringLengthAttribute(5));
 }
 public NameAttribute()
 {
     ValidationAttributes.Add(new MinLengthAttribute(5));
     ValidationAttributes.Add(new RequiredAttribute());
     ValidationAttributes.Add(new RegularExpressionAttribute(@"^[A-Z]+[a-zA-Z""'\s-]*$"));
 }