Completed
Last Updated: 29 Nov 2021 16:37 by ADMIN
Release R1 2022
Fabian
Created on: 28 Oct 2021 06:47
Category: MaskedEditBox
Type: Bug Report
0
RadValidationProvider: RadMaskedEditBox is not properly validated with the current Culture (e.g. "de-DE")

Use the following code snippet and enter "8,5" in RadMaskedEditBox. Then press Tab to navigate to the next control: 

        RadValidationRule validationRuleNumberLess;
        RadValidationRule validationRuleNumberGreater;
        float numberMaximum;
        float numberMinimum;

        public RadForm1()
        {
            InitializeComponent();
            CultureInfo culture = new System.Globalization.CultureInfo("de-DE");
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
            this.radMaskedEditBox1.Culture = culture;

            this.numberMaximum = float.Parse("9,999", culture);
            this.numberMinimum = 0;

            // set maximum
            this.validationRuleNumberLess = new RadValidationRule();
            this.validationRuleNumberLess.AddControl(this.radMaskedEditBox1);
            this.validationRuleNumberLess.Operator = Telerik.WinControls.Data.FilterOperator.IsLessThanOrEqualTo;
            this.validationRuleNumberLess.PropertyName = "Value";
            this.validationRuleNumberLess.ToolTipTitle = "Too big";
            this.validationRuleNumberLess.ToolTipText = "The value must be SMALLER than " + this.numberMaximum.ToString();
            this.validationRuleNumberLess.Value = (float)this.numberMaximum + 0.001D;
            this.radValidationProvider1.ValidationRules.Add(this.validationRuleNumberLess);
            this.radValidationProvider1.SetValidationRule(this.radMaskedEditBox1, this.validationRuleNumberLess);

            // set minimum
            this.validationRuleNumberGreater = new RadValidationRule();
            this.validationRuleNumberGreater.PropertyName = "Value";
            this.validationRuleNumberGreater.ToolTipTitle = "Too small"; // this.localToolTipTitle;
            this.validationRuleNumberGreater.ToolTipText = "The value must be BIGGER than " + this.numberMinimum.ToString();
            this.validationRuleNumberGreater.Operator = Telerik.WinControls.Data.FilterOperator.IsGreaterThanOrEqualTo;
            this.validationRuleNumberGreater.Value = (float)this.numberMinimum;
            this.validationRuleNumberGreater.AddControl(this.radMaskedEditBox1);
            this.radValidationProvider1.ValidationRules.Add(this.validationRuleNumberGreater);
            this.radValidationProvider1.SetValidationRule(this.radMaskedEditBox1, this.validationRuleNumberGreater);

            this.radLabel1.Text = "Minimum = " + this.validationRuleNumberGreater.Value + " Maximum = " + this.validationRuleNumberLess.Value;
        }

Workaround: use the ControlValidation event to control the validation flag: https://docs.telerik.com/devtools/winforms/controls/validation-provider/validation-rules#controlvalidation-event 

3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 03 Nov 2021 09:49

Hello, Roman,

Following the suggested approach for using the ControlValidation event in the previously referred section in the online documentation, I have prepared a sample code snippet to illustrate in a better way the idea behind the workaround.

You can add a RadValidationRule for empty Text for example and in addition to this handle the ControlValidation event which occurs before a RadEditorControl is being validated. The RadValidationEventArgs gives very useful information about the tooltip error indication, validation rule, etc. The IsValid argument allows you to override the default result indicating whether the validation fails and change the error message accordingly. The rest of the rules shouldn't be added in this case:

        float numberMaximum;
        float numberMinimum;

        public RadForm1()
        {
            InitializeComponent();
            CultureInfo culture = new System.Globalization.CultureInfo("de-DE");
            System.Threading.Thread.CurrentThread.CurrentCulture = culture;
            System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
            this.radMaskedEditBox1.Culture = culture;

            this.radMaskedEditBox1.MaskType = MaskType.Numeric;
            this.radMaskedEditBox1.Mask = "N";

            this.numberMaximum = float.Parse("9,999", culture);
            this.numberMinimum = 0;      

            RadValidationRule radValidationRule1 = new RadValidationRule();
            radValidationRule1.AutoToolTip = true;
            radValidationRule1.AddControl(this.radMaskedEditBox1);
            radValidationRule1.Operator = Telerik.WinControls.Data.FilterOperator.IsNotLike;
            radValidationRule1.PropertyName = "Text";
            radValidationRule1.ToolTipText = "Text is empty!";
            radValidationRule1.Value = "";
            radValidationProvider1.ValidationRules.Add(radValidationRule1);

            this.radLabel1.Text = "Minimum = " + numberMinimum + " Maximum = " + numberMaximum;

            this.radValidationProvider1.ControlValidation += RadValidationProvider1_ControlValidation;
        }

        private void RadValidationProvider1_ControlValidation(object sender, RadValidationEventArgs e)
        {
            if (e.Control == this.radMaskedEditBox1 && this.radMaskedEditBox1.Text != string.Empty)
            {
                e.IsValid = true;
                float value = 0;
                bool parsed = float.TryParse(this.radMaskedEditBox1.Text, out value);
                if (value>= numberMaximum)
                {
                    e.ErrorText = "The value must be SMALLER than " + this.numberMaximum.ToString();
                    e.IsValid = false;
                }
                else if (value<numberMinimum)
                {
                    e.ErrorText = "The value must be BIGGER than " + this.numberMinimum.ToString();
                    e.IsValid = false;
                }
               
            }
        }

I believe that it would fit your scenario. Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Fabian
Posted on: 28 Oct 2021 14:51

Hallo,

 

Now, I tested the DateTime MaskType and i got the same Exception, which i posted before.

I set the Mask to "dd/MM/yyyy" and the MaskType to DateTime. When I press TAB or switch to an other input field (with the mouse) i got these exception.

Telerik.Data.Expressions.InvalidExpressionException: "Cannot perform '<=' operation on System.DateTime and System.Int32."

 

LG

Roman

Fabian
Posted on: 28 Oct 2021 13:45

Hallo,

The ControlValidation helped me a lot! 

But now i got an other problem.

I set the Mask to "N" and the MaskType to "Numeric". Now i'm able to insert a number with the right local-culture-format ("123.456,78"). But when i submit the value (with TAB or ENTER) i get the following Exception in Programm.cs:

Telerik.Data.Expressions.InvalidExpressionException: "Cannot perform '<=' operation on System.String and System.Int32."

StackTrack:

   bei Telerik.Data.Expressions.Operator.BinaryCompare(Object value1, Object value2, StorageType resultType, Operator op, OperatorContext context)
   bei Telerik.Data.Expressions.Operator.LessOrEqualFunc(Operand lhs, Operand rhs, OperatorContext context)
   bei Telerik.Data.Expressions.BinaryOpNode.Eval(Object row, Object context)
   bei Telerik.WinControls.UI.RadValidationProvider.ValidateCore(Object sender, EventArgs e)
   bei Telerik.WinControls.UI.RadValidationProvider.AssociatedControl_Validating(Object sender, CancelEventArgs e)
   bei System.ComponentModel.CancelEventHandler.Invoke(Object sender, CancelEventArgs e)
   bei System.Windows.Forms.Control.OnValidating(CancelEventArgs e)
   bei System.Windows.Forms.Control.NotifyValidating()
   bei System.Windows.Forms.Control.PerformControlValidation(Boolean bulkValidation)
   bei System.Windows.Forms.ContainerControl.ValidateThroughAncestor(Control ancestorControl, Boolean preventFocusChangeOnError)
   bei System.Windows.Forms.ContainerControl.EnterValidation(Control enterControl)
   bei System.Windows.Forms.ContainerControl.UpdateFocusedControl()
   bei System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)
   bei System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)
   bei System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control)
   bei System.Windows.Forms.Control.WmSetFocus(Message& m)
   bei System.Windows.Forms.Control.WndProc(Message& m)
   bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   bei Telerik.WinControls.RadControl.WndProc(Message& m)
   bei Telerik.WinControls.UI.RadTitleBar.WndProc(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

I think the internal casting process doesn't work with the notation of the number (0.000.000,00). 

Is it possible to prevent this exception?

 

LG

Roman