示例#1
0
 /// <summary>
 /// Processes the specified model state.
 /// </summary>
 /// <param name="modelState">State of the model.</param>
 /// <param name="result">The result.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
 public static bool Process(this ModelStateDictionary modelState, IValidationContainer result)
 {
     foreach (var r in result.ValidationErrors)
         foreach (var e in r.Value)
             modelState.AddModelError(r.Key, e);
     return modelState.IsValid;
 }
示例#2
0
        public void RootSharesWithContainer()
        {
            //there's not really anything i can do with a container..
            //i need the scope
            ValidationContainer  toTest = ValidationContainer.CreateValidationContainer();
            IValidationContainer scoped = toTest.Scope("abc");

            scoped.AddValidation(new Validation("test", ValidationSeverity.Confirm, "hello"));
            Assert.Contains(toTest.Validations, t => t.Key.Equals("abc.test"));
        }
 public ValidationFieldHandler(IUIElement uiElement, string validationFiled, IValidationContainer validationContainer)
 {
     this.uiElement = uiElement;
     this.validationFiled = validationFiled;
     this.validationContainer = validationContainer;
     validationContainer.ValidationSourceChanged += OnValidationSourceChanged;
     validationContainer.ValidationEnabledChanged += OnValidationEnabledChanged;
     SetValidationSourceHandler();
     Debug.WriteLine(string.Format("Validation Handler constructed. validaton source: {0},  field {1} ", validationSource, uiElement));
 }
示例#4
0
        /// <summary>
        /// The server form is a defacto validation container
        /// </summary>
        /// <param name="ctl"></param>
        /// <returns></returns>
        private static bool IsValidationContainer(Control ctl)
        {
            if (ctl is HtmlForm)
            {
                return(true);
            }
            IValidationContainer val = ctl as IValidationContainer;

            return(val != null && val.IsValidationContainer);
        }
 /// <summary>
 /// Processes the specified model state.
 /// </summary>
 /// <param name="modelState">State of the model.</param>
 /// <param name="result">The result.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
 public static bool Process(this ModelStateDictionary modelState, IValidationContainer result)
 {
     foreach (var r in result.ValidationErrors)
     {
         foreach (var e in r.Value)
         {
             modelState.AddModelError(r.Key, e);
         }
     }
     return(modelState.IsValid);
 }
示例#6
0
        internal static void RegisterValidationContainer(IValidationContainer container)
        {
            HashSet <IValidationContainer> set = (HashSet <IValidationContainer>)HttpContext.Current.Items["KEY_CONTAINER"];

            if (set == null)
            {
                set = new HashSet <IValidationContainer>();
                HttpContext.Current.Items["KEY_CONTAINER"] = set;
            }
            set.Add(container);
        }
        /// <summary>
        /// Gets the MessageSource to be used for resolve error messages
        /// </summary>
        /// <remarks>
        /// By default, returns <see cref="FindValidationContainer"/>'s MessageSource.
        /// </remarks>
        /// <returns>the <see cref="IMessageSource"/> to resolve message texts. May be <c>null</c></returns>
        protected virtual IMessageSource ResolveMessageSource()
        {
            IMessageSource messageSource = this.MessageSource;

            if (messageSource == null)
            {
                IValidationContainer validationContainer = FindValidationContainer();
                messageSource = (validationContainer == null)
                                ? null
                                : validationContainer.MessageSource;
            }
            return(messageSource);
        }
        /// <summary>
        /// Gets the list of validation errors to render
        /// </summary>
        /// <returns>the <see cref="IValidationErrors"/> to render. May be <c>null</c></returns>
        protected virtual IValidationErrors ResolveValidationErrors()
        {
            IValidationErrors validationErrors = this.ValidationErrors;

            if (validationErrors == null)
            {
                IValidationContainer container = this.FindValidationContainer();
                if (container != null)
                {
                    validationErrors = container.ValidationErrors;
                }
            }
            return(validationErrors);
        }
示例#9
0
        private Control GetValidationContainer()
        {
            IValidationContainer container = this.Ancestors(false).OfType <IValidationContainer>().FirstOrDefault(p => p.IsValidationContainer);
            Control ctlContainer;

            if (container == null)
            {
                ctlContainer = this.Page.Form;
            }
            else
            {
                ctlContainer = (Control)container;
            }
            return(ctlContainer);
        }
        /// <summary>
        /// Gets the <see cref="IValidationContainer"/>, who's <see cref="IValidationContainer.ValidationErrors"/>
        /// shall be rendered by this control.
        /// </summary>
        /// <remarks>
        /// First, it tries to resolve the specified <see cref="ValidationContainerName"/>, if any. If no explicit name
        /// is set, will probe the control hierarchy for controls implementing <see cref="IValidationContainer"/>.
        /// </remarks>
        protected virtual IValidationContainer FindValidationContainer()
        {
            // is an explicit container specified?
            if (ValidationContainerName != null && ValidationContainerName.Length > 0)
            {
                Control start         = this.NamingContainer;
                string  containerName = this.ValidationContainerName;
                // shall we do a global search?
                if (containerName.StartsWith("::"))
                {
                    containerName = containerName.Substring(2);
                    start         = this.Page;
                }
                IValidationContainer container = start as IValidationContainer;
                if (containerName.Length > 0)
                {
                    container = start.FindControl(containerName) as IValidationContainer;
                }
                if (container == null)
                {
                    throw new ArgumentException(
                              string.Format(
                                  "Validation Container Control specified by {0} does not exist or does not implement IValidationContainer",
                                  this.ValidationContainerName));
                }
                return(container);
            }

            for (Control parent = this.Parent; parent != null; parent = parent.Parent)
            {
                IValidationContainer container = parent as IValidationContainer;
                if (container != null &&
                    container.ValidationErrors != null)
                {
                    return(container);
                }
            }
            return(null);
        }