Best way to validate form

11 posts, 0 answers
  1. Alan
    Alan avatar
    107 posts
    Member since:
    Oct 2010

    Posted 25 Apr 2011 Link to this post

    Hi,

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

    Many thanks,
    Daryl
  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 27 Apr 2011 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Alan
    Alan avatar
    107 posts
    Member since:
    Oct 2010

    Posted 27 Apr 2011 Link to this post

    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
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 28 Apr 2011 Link to this post

    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
  6. Marcelo
    Marcelo avatar
    40 posts
    Member since:
    Jul 2011

    Posted 16 Aug 2011 Link to this post

    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
  7. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 17 Aug 2011 Link to this post

    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 >>

  8. Marcelo
    Marcelo avatar
    40 posts
    Member since:
    Jul 2011

    Posted 17 Aug 2011 Link to this post

    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

  9. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 22 Aug 2011 Link to this post

    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 >>

  10. Marcelo
    Marcelo avatar
    40 posts
    Member since:
    Jul 2011

    Posted 22 Aug 2011 Link to this post

    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.
  11. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 23 Aug 2011 Link to this post

    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 >>

  12. Marcelo
    Marcelo avatar
    40 posts
    Member since:
    Jul 2011

    Posted 23 Aug 2011 Link to this post

    Thank you
Back to Top
UI for WPF is Visual Studio 2017 Ready