in

CodeSmith Community

Your Code. Your Way. Faster!

Validation problems for new records (winforms)

Last post 05-19-2008 4:55 PM by fweeee. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 05-16-2008 1:03 AM

    • fweeee
    • Top 25 Contributor
    • Joined on 08-08-2007
    • Posts 164
    • Points 4,103

    Validation problems for new records (winforms)

     I'm having some problems with validation in new records in my winforms project. In most of the tables of my database, I have at least one value which I am not allowing to be blank. I have written rules, either into the entity layer, or the service layer, to enforce this.

    The problem I'm having, is when I create a new record and try to edit it, these rules are getting in the way. The basica workflow for creating a new record is:

    1) Click on an "Add New" button.

    2) New record is created.

    3) A card (detail) view form is opened up where the user can edit it. 

    The problem is, when the card opens, the validation rules for entity have kicked in. And seeing as though all of the fields are empty (it is a new record afterall), some of the fields are in error. As I'm using the  ErrorProvider control (with the flashing red dot thing on), several fields have the red error dot next to them as soon as the user opens it up.

    Is there some way around this? I'd prefer if the fields in the new entity are not valided until they are modified, and the whole thing when the user tries to save it. 

    I'm not 100% sure, but from memory NetTiers did not always behave like this. 

    I'm trying to think of a work around, and the best one I can think of so far is to turn the flashing red dots off (on the ErrorProvider control). But I'd prefer if I had great control over the entities themselves.

    Thanks. 

    ---- EDIT ----

    I have had a bit more of a think about this, and the work around I thought of (turning off flashing red dots) wont work. If the user clicks into an empty field (which is in error, as it wants a value), it still wont let them leave. Very annoying.

    Idealy what I would want is:

    1) New record is created with no errors.

    2) As fields are modified, that field only is validated (other fields which might be invalid dont effect it).

    3) When the user goes to save the new record, all validation rules are taken into account.

    Any assistance in acheiving this would be appreciated. 

    • Post Points: 35
  • 05-19-2008 9:28 AM In reply to

    • GRAW
    • Top 25 Contributor
    • Joined on 06-23-2006
    • Posts 157
    • Points 4,560

    Re: Validation problems for new records (winforms)

     As a workaround, you may want to look into clearing the ValidationRules collection while you're doing the editing, then re-add all the ValidationRules (by calling entity.AddValidationRules() on the entity object) when you're ready to save.  This is from memory, so it may not be completely correct, but the AddValidationRules method may be protected, if that is the case, you may want to add some additional public wrapper methods which will perform this functionality for you.

    You may also want to look into setting the SupressEntityEvents property on the entity.  Set this before starting your edit, then reset it back just before you save it.  I'm not sure if this will work, give it a try. 

    "Small is the number of them that see with their own eyes, and feel with their own hearts" Albert Einstein
    • Post Points: 35
  • 05-19-2008 4:55 PM In reply to

    • fweeee
    • Top 25 Contributor
    • Joined on 08-08-2007
    • Posts 164
    • Points 4,103

    Re: Validation problems for new records (winforms)

     Thanks. I thought of doing something like that (but wasnt too sure how I would implement it). I thought it might get a bit messy for a number of reasons:

    - I would be getting rid of *all* the validation rules, not just the few that were causing problems

    - as I use the same forms for both modifying and creating, I would need to write the code in all of them to handle this.

    In the end, I decided to go with this approach:

    - Modify the "Field must have a value" validation rules so they only kick in if the record is not new (IsNew is false)

    - Add a property to the EntityBase class called "IsNewError" (returns a string, but is null by defalt)

    - Add overrides to any entities that require it (they have fields which cannot be blank).

    - Before I save a record, I check "IsNewError" (If the record is new). I dont save if this returns a string. (and show the string - its an error message).

    This does not seem to me to be a very elegant way of dealing with it, but in the absense of a better idea, its going to have to do. 

    • Post Points: 5
Page 1 of 1 (3 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems