/// <summary>
        /// Initializes a new instance of the <see cref="AngularDataAnnotationsModelValidator"/> class.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The AngularJS binding context.</param>
        /// <param name="attribute">The validation attribute for the model.</param>
        /// <exception cref="System.ArgumentNullException">attribute is <code>null</code></exception>
        public AngularDataAnnotationsModelValidator(ModelMetadata metadata, AngularBindingContext context, ValidationAttribute attribute)
            : base(metadata, context)
        {
            if (attribute == null)
            {
                throw new ArgumentNullException("attribute");
            }

            Attribute = attribute;
        }
Esempio n. 2
0
        /// <summary>
        /// Called from constructors in derived classes to initialize the <see cref="AngularModelValidator"/> class.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The AngularJS binding context.</param>
        /// <exception cref="System.ArgumentNullException">
        /// metadata is <c>null</c>
        /// or
        /// context is <c>null</c>
        /// </exception>
        protected AngularModelValidator(ModelMetadata metadata, AngularBindingContext context)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            Metadata = metadata;
            Context  = context;
        }
        /// <summary>
        /// Gets a list of validators.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The AngularJS binding context.</param>
        /// <param name="attributes">The list of validation attributes.</param>
        /// <returns>A list of validators.</returns>
        protected override IEnumerable <AngularModelValidator> GetValidators(ModelMetadata metadata,
                                                                             AngularBindingContext context, IEnumerable <Attribute> attributes)
        {
            AdaptersLock.EnterReadLock();

            try
            {
                var results = new List <AngularModelValidator>();

                // Add an implied [Required] attribute for any non-nullable value type,
                // unless they've configured us not to do that.
                if (AddImplicitRequiredAttributeForValueTypes &&
                    metadata.IsRequired &&
                    !attributes.Any(a => a is RequiredAttribute))
                {
                    attributes = attributes.Concat(new[] { new RequiredAttribute() });
                }

                // Produce a validator for each validation attribute we find
                foreach (ValidationAttribute attribute in attributes.OfType <ValidationAttribute>())
                {
                    AngularDataAnnotationsModelValidationFactory factory;
                    if (!AttributeFactories.TryGetValue(attribute.GetType(), out factory))
                    {
                        factory = DefaultAttributeFactory;
                    }
                    results.Add(factory(metadata, context, attribute));
                }

                return(results);
            }
            finally
            {
                AdaptersLock.ExitReadLock();
            }
        }
Esempio n. 4
0
 public DoubleAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, DoubleAttribute attribute)
     : base(metadata, context, attribute)
 {
 }
 public DataTypeAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, DataTypeAttribute attribute, string ruleName)
     : base(metadata, context, attribute)
 {
     RuleName  = ruleName;            //?? attribute.DataType.ToString().ToLower(CultureInfo.CurrentCulture);
     _dataType = attribute.DataType;
 }
 public CompareAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, DataAnnotationsCompareAttribute attribute)
     : base(metadata, context, new CompareAttributeWrapper(attribute, metadata))
 {
 }
 /// <summary>
 /// Gets a list of validators.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 public abstract IEnumerable <AngularModelValidator> GetValidators(ModelMetadata metadata, AngularBindingContext context);
 private IEnumerable <AngularModelValidator> GetValidatorsForType(ModelMetadata metadata, AngularBindingContext context)
 {
     return(GetValidators(metadata, context, GetTypeDescriptor(metadata.ModelType).GetAttributes().OfType <Attribute>()));
 }
        private IEnumerable <AngularModelValidator> GetValidatorsForProperty(ModelMetadata metadata, AngularBindingContext context)
        {
            ICustomTypeDescriptor typeDescriptor = GetTypeDescriptor(metadata.ContainerType);
            PropertyDescriptor    property       = typeDescriptor.GetProperties().Find(metadata.PropertyName, true);

            if (property == null)
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Properties.Resources.Common_PropertyNotFound,
                              metadata.ContainerType.FullName, metadata.PropertyName),
                          "metadata");
            }
            return(GetValidators(metadata, context, property.Attributes.OfType <Attribute>()));
        }
 /// <summary>
 /// Gets the validators for the model using the metadata, the controller context, and a list of attributes.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 /// <param name="attributes">The list of attributes.</param>
 /// <returns>The validators for the model.</returns>
 protected abstract IEnumerable <AngularModelValidator> GetValidators(ModelMetadata metadata, AngularBindingContext context, IEnumerable <Attribute> attributes);
        /// <summary>
        /// Gets a list of validators.
        /// </summary>
        /// <param name="metadata">The model metadata.</param>
        /// <param name="context">The AngularJS binding context.</param>
        /// <exception cref="System.ArgumentNullException">metadata</exception>
        public sealed override IEnumerable <AngularModelValidator> GetValidators(ModelMetadata metadata, AngularBindingContext context)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            if (metadata.ContainerType != null && !String.IsNullOrEmpty(metadata.PropertyName))
            {
                return(GetValidatorsForProperty(metadata, context));
            }

            return(GetValidatorsForType(metadata, context));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="StringLengthAttributeAdapter"/> class.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 /// <param name="attribute">The string-length attribute.</param>
 public StringLengthAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, StringLengthAttribute attribute)
     : base(metadata, context, attribute)
 {
 }
 /// <summary>
 /// Returns the list of model validators.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 public IEnumerable <AngularModelValidator> GetValidators(ModelMetadata metadata, AngularBindingContext context)
 {
     for (int i = 0; i < Items.Count; i++)
     {
         AngularModelValidatorProvider provider = Items[i];
         foreach (AngularModelValidator validator in provider.GetValidators(metadata, context))
         {
             yield return(validator);
         }
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RegularExpressionAttributeAdapter"/> class.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 /// <param name="attribute">The regular expression attribute.</param>
 public RegularExpressionAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, RegularExpressionAttribute attribute)
     : base(metadata, context, attribute)
 {
 }
Esempio n. 15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RequiredAttributeAdapter"/> class.
 /// </summary>
 /// <param name="metadata">The model metadata.</param>
 /// <param name="context">The AngularJS binding context.</param>
 /// <param name="attribute">The required attribute.</param>
 public RequiredAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, RequiredAttribute attribute)
     : base(metadata, context, attribute)
 {
 }
 public EmailAddressAttributeAdapter(ModelMetadata metadata, AngularBindingContext context, EmailAddressAttribute attribute)
     : base(metadata, context, attribute)
 {
 }