예제 #1
0
        /// <summary>
        /// Creates and sets new Validation Rule(inherited from <see cref="IValidationRule"/>).
        /// </summary>
        public void AssignValidationRule()
        {
            RangeValidationRule rule = new RangeValidationRule(NumericQuestion.Types.IntegerType, null, null);

            rule.RegexRule = new RegexValidationRule("");
            ValidationRule = rule;
        }
        public void Validate_WithTestClass(int value, int minValue, int maxValue, bool expected)
        {
            // Arrange
            var sut = new RangeValidationRule <RangeTestClass>(ERROR_MESSAGE, new RangeTestClass(minValue), new RangeTestClass(maxValue));

            // Act
            var result = sut.Validate(new RangeTestClass(value));

            // Assert
            result.Should().Be(expected);
        }
        public void Validate_WithAllowedNullValue_Valid()
        {
            // Arrange
            var sut = new RangeValidationRule <string>(ERROR_MESSAGE, "0", "100");

            // Act
            var result = sut.Validate(null);

            // Assert
            result.Should().BeTrue();
        }
        public void Validate_WithNullValue_NotValid()
        {
            // Arrange
            var sut = new RangeValidationRule <string>(ERROR_MESSAGE, "0", "100", nullIsValid: false);

            // Act
            var result = sut.Validate(null);

            // Assert
            result.Should().BeFalse();
        }
예제 #5
0
        public Page011_Validation()
        {
            InitializeComponent();



            Binding binding = new Binding("Value")
            {
                Source = this.sliTest
            };

            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            RangeValidationRule rvr = new RangeValidationRule()
            {
                Min = 0,
                Max = 100
            };


            // Binding 只在 Target 被外部方法更新是 校验数据,而自身 Binding 的 Source 数据更新 Target 时是不进行校验的。
            // 如果要改变这种行为, 需要增加下面 ValidatesOnTargetUpdated = true 的处理。
            rvr.ValidatesOnTargetUpdated = true;


            binding.ValidationRules.Add(rvr);



            // 默认情况下, 数据错误时,仅仅是 输入框 边框线变红色。
            // 如果要求有 自定义的 错误显示方式. 需要自己定义一个事件处理.
            binding.NotifyOnValidationError = true;


            this.txtTest.SetBinding(TextBox.TextProperty, binding);



            // 默认情况下, 数据错误时,仅仅是 输入框 边框线变红色。
            // 如果要求有 自定义的 错误显示方式. 需要自己定义一个事件处理.
            this.txtTest.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(this.ValidationError));
        }
예제 #6
0
 private bool ValidatePassword()
 {
     if (_viewModel.IsEncryptionEnabled)
     {
         RangeValidationRule rule = new RangeValidationRule(NumericQuestion.Types.IntegerType, null, null);
         rule.RegexRule = new RegexValidationRule("");
         string newValue = Password.Text;
         Password.IsValid = true;
         Password.IsValid = rule.Validate(newValue);
         if (!Password.IsValid)
         {
             Password.ValidationContent = Languages.AppResources.validationRules_LengthEmptyMessage;
             return(false);
         }
         else
         {
             return(true);
         }
     }
     return(true);
 }
예제 #7
0
        private void OnCustomValueChanged(object sender, RoutedEventArgs e)
        {
            if (((ValidationControl)sender).Name == "FontSize")
            {
                int minValue             = 15;
                int maxValue             = 35;
                RangeValidationRule rule = new RangeValidationRule(NumericQuestion.Types.IntegerType, minValue, maxValue);

                string newValue = ((ValidationControl)sender).Text;
                ((ValidationControl)sender).IsValid = true;
                ((ValidationControl)sender).IsValid = rule.Validate(newValue);
                if (((ValidationControl)sender).IsValid)
                {
                    OperationsOnSettings.Instance.FontSize = Convert.ToInt32(newValue);
                    _oldFontSize = OperationsOnSettings.Instance.FontSize;
                    LoadSettingsPageContent();
                }
                else
                {
                    ((ValidationControl)sender).ValidationContent = string.Format(Languages.AppResources.settingsPage_InvalidCustomValue, minValue.ToString(), maxValue.ToString());
                }
            }
        }