This is a migrated thread and some comments may be shown as answers.

DataFormDataField validation

2 Answers 252 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 11 Apr 2012, 11:39 PM
I have a RadDataForm, with a DataFormDataField, that is bound to a Decimal property.

I'm trying to get validation working, and I'm having some issues:

  1. When I have a validation error, and the DataFormDataField does not have its Label property set, the error is displayed using the name of the field, instead of the [Display(Name="xxx")] annotation.
  2. When I enter invalid characters (non-numeric), my Decimal property's setter is never called, instead the form displays a "Value 'xxx' could not be converted" error. I need to display a more meaningful message.
  3. I have a listener on the RadDataForm's ValidatingItem event - but that event is not called when I move focus from one field to another (which is when the above error messages appear.)
  4. I need to some level of validation on each keystroke. If the field can only hold 12 characters, an attempt to type a 13th should do nothing. If the user is typing in a field that can only hold numeric values, typing non-numeric characters should do nothing - the keyboard event should be swallowed, and the character should not appear in the field.  At a minimum, letters would be filtered, it'd be better if we could look at the new character and the existing content, and decide whether the new character can be applied without invalidating the field.  (If the field is bound to a decimal, it should allow numbers, and one decimal point. A second decimal point should be swallowed.)

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 16 Apr 2012, 04:14 PM
Hi Jeffrey,

Firstly, I would recommend you to run through this article for a reference. Depending on the validation that you do, you can easily set ErrorMessage attribute.  
Considering the validation of a numeric value when a string one is entered, this is handled by the Framework, not by RadDataForm - that is why the ValidatingItem event is not fired. Please run through this and this articles for a reference. 
As for your last requirement, you can try implementing some custom logic in TextChanged event of the TextBox:

public MainWindow()
        {
            InitializeComponent();
             
            this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(OnTextChanged), true);
        }
 
        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
         
        }

Another possible approach would be to implement the logic inside the setter of the property. 


Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jeff
Top achievements
Rank 1
answered on 16 Apr 2012, 05:21 PM
Some solutions I've found:

  2. ValidationRules can be configured to, and by default are, run prior to the conversion to the datatype of the bound variable.
  4. If you set UpdateSourceTrigger = PropertyChanged on a binding, the value will be validated every time it is changed. (The default is LostFocus).

Still no luck on #1.
Tags
DataForm
Asked by
Jeff
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jeff
Top achievements
Rank 1
Share this question
or