示例#1
0
        /// <summary>
        /// Sets the exception.
        /// </summary>
        /// <param name="error">The error.</param>
        internal void SetException(DataValidationErrors error)
        {
            ErrorType = error;
            string message = "Unknown error";

            switch (error)
            {
            case DataValidationErrors.DataIntegrityValidationError:
                message = "Data integrity is compromised.";
                break;

            case DataValidationErrors.DataIsTooShort:
                message = "Data does not appear to be encrypted by EasyCrypto";
                break;

            case DataValidationErrors.InvalidMagicNumber:
                message = "Data does not appear to be encrypted by EasyCrypto";
                break;

            case DataValidationErrors.KeyCheckValueValidationError:
                message = "Key/IV or password is not valid";
                break;

            case DataValidationErrors.UnsupportedDataVersion:
                message = "Data is encrypted by newer version of the EasyCrypto";
                break;
            }
            ExceptionToThrow = new DataFormatValidationException(message, error);
            ErrorMessage     = message;
        }
    protected override void OnAttached(CompositeDisposable disposables)
    {
        if (AssociatedObject is null)
        {
            return;
        }

        var hasErrors = AssociatedObject.GetObservable(DataValidationErrors.HasErrorsProperty);
        var text      = AssociatedObject.GetObservable(TextBox.TextProperty);

        hasErrors.Select(_ => Unit.Default)
        .Merge(text.Select(_ => Unit.Default))
        .Throttle(TimeSpan.FromMilliseconds(100))
        .ObserveOn(RxApp.MainThreadScheduler)
        .Subscribe(_ =>
        {
            if (AssociatedObject is { } &&
                !DataValidationErrors.GetHasErrors(AssociatedObject) &&
                !string.IsNullOrEmpty(AssociatedObject.Text) &&
                KeyboardNavigationHandler.GetNext(AssociatedObject, NavigationDirection.Next) is
                { } nextFocus)
            {
                nextFocus.Focus();
            }
        })
 /// <summary>
 /// Called to update the validation state for properties for which data validation is
 /// enabled.
 /// </summary>
 /// <param name="property">The property.</param>
 /// <param name="value">The new binding value for the property.</param>
 protected override void UpdateDataValidation <T>(AvaloniaProperty <T> property, BindingValue <T> value)
 {
     if (property == SelectedItemProperty)
     {
         DataValidationErrors.SetError(this, value.Error);
     }
 }
 /// <summary>
 /// Called to update the validation state for properties for which data validation is
 /// enabled.
 /// </summary>
 /// <param name="property">The property.</param>
 /// <param name="state">The current data binding state.</param>
 /// <param name="error">The current data binding error, if any.</param>
 protected override void UpdateDataValidation(
     AvaloniaProperty property,
     BindingValueType state,
     Exception?error)
 {
     if (property == SelectedItemProperty)
     {
         DataValidationErrors.SetError(this, error);
     }
 }
        public void SelectedItem_Validation()
        {
            RunTest((control, textbox) =>
            {
                var exception      = new InvalidCastException("failed validation");
                var itemObservable = new BehaviorSubject <BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
                control.Bind(AutoCompleteBox.SelectedItemProperty, itemObservable);
                Dispatcher.UIThread.RunJobs();

                Assert.Equal(DataValidationErrors.GetHasErrors(control), true);
                Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true);
            });
        }
        public void Value_Validation()
        {
            RunTest((control, textbox) =>
            {
                var exception       = new InvalidCastException("failed validation");
                var valueObservable = new BehaviorSubject <BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
                control.Bind(NumericUpDown.ValueProperty, valueObservable);
                Dispatcher.UIThread.RunJobs();

                Assert.True(DataValidationErrors.GetHasErrors(control));
                Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }));
            });
        }
示例#7
0
        private async void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            DataValidationErrors.ClearErrors(NameBox);
            bool isInvalid = false;

            if (string.IsNullOrWhiteSpace(TempProfile.Name))
            {
                DataValidationErrors.SetError(NameBox, new DataValidationException(LocaleManager.Instance["UserProfileEmptyNameError"]));

                isInvalid = true;
            }

            if (TempProfile.Image == null)
            {
                await ContentDialogHelper.CreateWarningDialog(LocaleManager.Instance["UserProfileNoImageError"], "");

                isInvalid = true;
            }

            if (isInvalid)
            {
                return;
            }

            if (_profile != null)
            {
                _profile.Name  = TempProfile.Name;
                _profile.Image = TempProfile.Image;
                _profile.UpdateState();
                _parent.AccountManager.SetUserName(_profile.UserId, _profile.Name);
                _parent.AccountManager.SetUserImage(_profile.UserId, _profile.Image);
            }
            else if (_isNewUser)
            {
                _parent.AccountManager.AddUser(TempProfile.Name, TempProfile.Image);
            }
            else
            {
                return;
            }

            _parent?.GoBack();
        }
        public void Setter_Exceptions_Should_Set_DataValidationErrors_HasErrors()
        {
            using (UnitTestApplication.Start(Services))
            {
                var target = new TextBox
                {
                    DataContext             = new ExceptionTest(),
                    [!TextBox.TextProperty] = new Binding(nameof(ExceptionTest.LessThan10), BindingMode.TwoWay),
                    Template = CreateTemplate(),
                };

                target.ApplyTemplate();

                Assert.False(DataValidationErrors.GetHasErrors(target));
                target.Text = "20";
                Assert.True(DataValidationErrors.GetHasErrors(target));
                target.Text = "1";
                Assert.False(DataValidationErrors.GetHasErrors(target));
            }
        }
示例#9
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            var keymap = AvaloniaLocator.Current.GetService <PlatformHotkeyConfiguration>();

            if (keymap.Paste.Any(g => g.Matches(e)))
            {
                CustomPaste();
                e.Handled = true;
            }
            else
            {
                base.OnKeyDown(e);
            }

            if (Text == "" && DataValidationErrors.GetHasErrors(this))
            {
                Text = "0";
                SelectAll();
            }
        }
示例#10
0
        public void SelectedItem_Validation()
        {
            using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
            {
                var target = new ComboBox
                {
                    Template           = GetTemplate(),
                    VirtualizationMode = ItemVirtualizationMode.None
                };

                target.ApplyTemplate();
                target.Presenter.ApplyTemplate();

                var exception      = new System.InvalidCastException("failed validation");
                var textObservable = new BehaviorSubject <BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
                target.Bind(ComboBox.SelectedItemProperty, textObservable);

                Assert.True(DataValidationErrors.GetHasErrors(target));
                Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
            }
        }
示例#11
0
        public void SelectedItem_Validation()
        {
            var target = new ListBox
            {
                Template           = ListBoxTemplate(),
                Items              = new[] { "Foo" },
                ItemTemplate       = new FuncDataTemplate <string>((_, __) => new Canvas()),
                SelectionMode      = SelectionMode.AlwaysSelected,
                VirtualizationMode = ItemVirtualizationMode.None
            };

            Prepare(target);

            var exception      = new System.InvalidCastException("failed validation");
            var textObservable = new BehaviorSubject <BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));

            target.Bind(ComboBox.SelectedItemProperty, textObservable);

            Assert.True(DataValidationErrors.GetHasErrors(target));
            Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
        }
        public void Setter_Exceptions_Should_Set_DataValidationErrors_Errors()
        {
            using (UnitTestApplication.Start(Services))
            {
                var target = new TextBox
                {
                    DataContext             = new ExceptionTest(),
                    [!TextBox.TextProperty] = new Binding(nameof(ExceptionTest.LessThan10), BindingMode.TwoWay),
                    Template = CreateTemplate(),
                };

                target.ApplyTemplate();

                Assert.Null(DataValidationErrors.GetErrors(target));
                target.Text = "20";

                IEnumerable <object> errors = DataValidationErrors.GetErrors(target);
                Assert.Single(errors);
                Assert.IsType <InvalidOperationException>(errors.Single());
                target.Text = "1";
                Assert.Null(DataValidationErrors.GetErrors(target));
            }
        }
示例#13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataFormatValidationException"/> class.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="error">The error.</param>
 public DataFormatValidationException(string message, DataValidationErrors error) : base(message)
 {
     ValidationError = error;
 }
示例#14
0
            /// <summary>
            /// Creates failed <see cref="ValidateAddressDataResponse"/> response.
            /// </summary>
            /// <param name="errorCode">The error code associated with the failed address component.</param>
            /// <param name="faultAddressComponent">The failed address component.</param>
            /// <returns>The <see cref="ValidateAddressDataResponse"/> response.</returns>
            private static ValidateAddressDataResponse CreateFailedValidateAddressDataResponse(DataValidationErrors errorCode, string faultAddressComponent)
            {
                // If address is not valid, tell the user/client code : which component is the faulty one
                var message = string.Format(CultureInfo.InvariantCulture, @"Incorrect address provided: validate {0} property.", faultAddressComponent);

                NetTracer.Information(message);

                // create the response object and return
                return(new ValidateAddressDataResponse(isAddressValid: false, invalidAddressComponentName: faultAddressComponent, errorCode: errorCode, errorMessage: message));
            }