Ejemplo n.º 1
0
        private void SetErrors(object target, IList <object> errors, bool isClear)
        {
            var hasErrors = errors.Count != 0;

            if (TryGetEntryField != null)
            {
                target = TryGetEntryField(target);
            }

            var nativeObject = target as INativeObject;

            if (!nativeObject.IsAlive())
            {
                return;
            }
            var uiView = target as UIView;

            if (uiView != null && ErrorBorderWidth > 0)
            {
                if (hasErrors)
                {
                    LayoutInfo.Save(uiView, target);
                    uiView.Layer.BorderColor  = ErrorBorderColor.CGColor;
                    uiView.Layer.BorderWidth  = ErrorBorderWidth;
                    uiView.Layer.CornerRadius = CornerRadius;
                    uiView.ClipsToBounds      = true;
                }
                else
                {
                    LayoutInfo.Restore(uiView, target);
                }
            }

            var textField = target as UITextField;

            if (textField != null && ErrorImage != null && textField.Superview != null)
            {
                ErrorButton         errorButton;
                UITextFieldViewMode mode = hasErrors ? UITextFieldViewMode.Always : UITextFieldViewMode.Never;
                if (RightErrorImagePosition)
                {
                    textField.RightViewMode = mode;
                    errorButton             = textField.RightView as ErrorButton;
                    if (isClear)
                    {
                        textField.RightView = null;
                    }
                    else if (errorButton == null)
                    {
                        errorButton         = CreateErrorButton(textField);
                        textField.RightView = errorButton;
                    }
                }
                else
                {
                    textField.LeftViewMode = mode;
                    errorButton            = textField.LeftView as ErrorButton;
                    if (isClear)
                    {
                        textField.LeftView = null;
                    }
                    else if (errorButton == null)
                    {
                        errorButton        = CreateErrorButton(textField);
                        textField.LeftView = errorButton;
                    }
                }

                if (isClear)
                {
                    if (errorButton != null)
                    {
                        errorButton.ClearBindingsRecursively(true, true);
                        errorButton.DisposeEx();
                    }
                }
                else
                {
                    errorButton.SetErrors(errors);
                }
            }
        }
        /// <summary>
        ///     Sets errors for binding target.
        /// </summary>
        /// <param name="target">The binding target object.</param>
        /// <param name="errors">The collection of errors</param>
        /// <param name="context">The specified context, if any.</param>
        protected override void SetErrors(object target, IList <object> errors, IDataContext context)
        {
            base.SetErrors(target, errors, context);

            var hasErrors = errors.Count != 0;

#if XAMARIN_FORMS
            var element = target as Element;
            if (element != null)
            {
                target = GetNativeView(element);
            }
#else
            var element = target as EntryElement;
            if (element != null && GetEntryField != null)
            {
                target = GetEntryField(element);
            }
#endif
            var uiView = target as UIView;
            if (uiView != null && ErrorBorderWidth > 0)
            {
                if (hasErrors)
                {
                    LayoutInfo.Save(uiView);
                    uiView.Layer.BorderColor  = ErrorBorderColor.CGColor;
                    uiView.Layer.BorderWidth  = ErrorBorderWidth;
                    uiView.Layer.CornerRadius = CornerRadius;
                    uiView.ClipsToBounds      = true;
                }
                else
                {
                    LayoutInfo.Restore(uiView);
                }
            }

            var textField = target as UITextField;
            if (textField != null && ErrorImage != null && textField.Superview != null)
            {
                ErrorButton         errorButton;
                UITextFieldViewMode mode = hasErrors ? UITextFieldViewMode.Always : UITextFieldViewMode.Never;
                if (RightErrorImagePosition)
                {
                    textField.RightViewMode = mode;
                    errorButton             = textField.RightView as ErrorButton;
                    if (errorButton == null)
                    {
                        errorButton         = CreateErrorButton(textField);
                        textField.RightView = errorButton;
                    }
                }
                else
                {
                    textField.LeftViewMode = mode;
                    errorButton            = textField.LeftView as ErrorButton;
                    if (errorButton == null)
                    {
                        errorButton        = CreateErrorButton(textField);
                        textField.LeftView = errorButton;
                    }
                }
                errorButton.SetErrors(errors);
            }
        }