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

Best way to validate form

10 Answers 374 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 25 Apr 2011, 09:59 PM
Hi,

Could you please tell me what the best way to validating the form is? (and displaying error messages/highlighting fields)

Many thanks,
Daryl

10 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 27 Apr 2011, 02:16 PM
Hi Daryl,

I am sending you a sample project illustrating a basic validation of the RadDataForm. You may test the case by inserting a string into the Salary field and the validation error will be displayed.
Let me know in case your scenario is not the same and if so, please provide more information about your requirements.
 

Greetings,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alan
Top achievements
Rank 1
answered on 27 Apr 2011, 10:00 PM
Hi Maya,

That's not exactly what I wanted, since I'm using Entity Framework, the classes already implement INotifyPropertyChanged. However the kind of validation I wish to implement is more of a business logic kind of validation. For example "Username not available" or "Name cannot be left empty".

Many Thanks,
Daryl
0
Maya
Telerik team
answered on 28 Apr 2011, 08:46 AM
Hello Daryl,

I am sending you an updated version of the sample illustrating how you may implement custom logic for validating the values.  Furthermore, you may handle the ValidatingItem event, perform your own validation and cancel the default one.

Best wishes,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Marcelo
Top achievements
Rank 1
answered on 16 Aug 2011, 03:50 PM
Hello Maya. I have followed your code sample but I am still not able to make it work. Data Type errors are throwing an exception. Here is what I do. I edit a record, I enter text in a int field and I get a System.FormatException of "Input string was not in a correct format."

Also, I am trying to understand why we need DataForm[XXX]Field controls if I am not changing the layout of the form. In my mind, the RadDataForm control should handle data type validations (w/o any extra code) and business rules validations. Now on this last one, if we implement these business rules validations inside the property setter (by throwing an exception), how do we catch it?

Validation Example Page
0
Maya
Telerik team
answered on 17 Aug 2011, 01:50 PM
Hi Marcelo,

The first issue you mentioned is a validation handled by the Framework, not by the data form. You are not allowed to enter string values into numeric fields. 
Considering the second issue, you may predefine the data fields in case setting the corresponding validation properties in the data binding is required. If you want to handle the validation, you may subscribe to ValidatingItem event. Still, if the logic is implemented in the setter, you are not obliged to handle it.

 

All the best,
Maya
the Telerik team

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

0
Marcelo
Top achievements
Rank 1
answered on 17 Aug 2011, 02:59 PM
Hello Maya and thank you for the follow up.

I agree that we "are not supposed to enter string values into numeric fields", but this is a valid test and I need to know how these controls behave in an event like this. After more testing yesterday, it looks like the execution of the code breaks (and I get an exception) because I am running the app via the VS 2010 IDE. If I click "Continue" a few times, then I see the WPF form displaying a message at the bottom. So this would be OK I guess when the app has been compiled and running on its own.

Now the business rules. There is a difference between "handling" these rules in the setter properties compared to the 

_ValidatingItem 

event. The code in the setter properties fires right away and if the preferred method is throwing an exception (like I see in every example) I still don't know where these exceptions can be caught. Now, if we use the _ValidatingItem event, this event does not trigger until the OK button is clicked.

All the examples I have seen so far don't work the way I would expect. If you have code samples that show Telerik's preferred way of doing validation (both in the setter properties and within the _ValidatingItem event), I would ask you to please post them.

Thank you

0
Maya
Telerik team
answered on 22 Aug 2011, 03:44 PM
Hi Marcelo,

The most appropriate approach depends entirely on your specific requirements. May you clarify what is the behavior that you expect ? What are the validation that you want to implement ?
 

Greetings,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Marcelo
Top achievements
Rank 1
answered on 22 Aug 2011, 05:43 PM
Hello Maya.

Now that the data type errors seem to be handled correctly, we can move on to the other two scenarios. The nice thing about putting some validation in the setter properties is that they are triggered right away (instant feedback). The _ValidatingItem event gets triggered when the Click button is clicked (like a Page.IsValid in ASP.NET)

Now, what I just discovered is that if I am running the app thru VS, the code stops, reporting an exception (and it does not run pass that point). If I deploy, it works as I would expect. This was the missing link for me. Now the question is why the different behavior.
0
Maya
Telerik team
answered on 23 Aug 2011, 11:49 AM
Hi Marcelo,

The inner exception thrown in this case will be caught only in debug mode, when the Common Language Runtime Exception are enabled. However, this exception will be swallowed in release mode and you will get the validation error in the RadDataForm. You will get the same behavior with DataForm as well.  

  

Greetings,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Marcelo
Top achievements
Rank 1
answered on 23 Aug 2011, 03:40 PM
Thank you
Tags
DataForm
Asked by
Alan
Top achievements
Rank 1
Answers by
Maya
Telerik team
Alan
Top achievements
Rank 1
Marcelo
Top achievements
Rank 1
Share this question
or