Print Page | Close Window

Validation Problem

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4191
Printed Date: 05-Jun-2024 at 6:03am


Topic: Validation Problem
Posted By: mariusk
Subject: Validation Problem
Date Posted: 23-Jun-2013 at 2:07am
I am very new in starting out in with devforce. I am busy going through the validation code samples situated at  http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-validation-wpf - http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-validation-wpf .


I have downloaded the sample project and configured the connection string to point to my local northwindIB database.

When I click on the Add button and then directly on OK I expect the First Name textbox to be highlighted in red or to have an exclamation mark next to it as it is an compulsory field.

It doesn't, it goes all the way through to the save method and then continues to execute _mgr.SaveChanges();

Then this function throws an "EntityManagerSaveException was unhandled" exception.

What do I need to add to this sample project to stop it from executing the save method before all the required text boxes have been populated?

Isn't the purpose of this particular example to show how the validation works???


It's also called WPF_SimpleValidationCompleted which would indicate to me that I do not need to add extra code to this project for it to show me how the validation features work?

Please help, I just need a working example as to how I can validate a UI connected to an entity framework model.

Regards
Marius



Replies:
Posted By: sbelini
Date Posted: 26-Jun-2013 at 12:47pm
Mariusk,

The sample is rather simple and just gives you an idea of how validation works.
Obviously, you can always add complexity to it.

The validation on the sample is meant to happen to existing records in case you modify them with invalid data. 
i.e. load one of the records and delete the FirstName value; as soon as you tab off the field, the validation will occur and complain about the problem (null value).

If you want to avoid the exception when you call SaveChanges on a newly created entity that hasn't had its values assigned, simply add some checking before save:

public void Save()
        {
          var result = _mgr.VerifierEngine.Execute(_currentEmployee);
          if (!result.HasErrors) {
            _mgr.SaveChanges();
          } else {
            MessageBox.Show(result.Errors.First().Message);
          }
        }
Notice that now the SaveChanges call won't occur unless all entities to be saved conform with validation rules. Also, if you navigate among the entities, you will notice that the required field in the new entity (i.e. First and LastName) will have the exclamation mark.





Print Page | Close Window