New Posts New Posts RSS Feed: Validation Problem
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Validation Problem

 Post Reply Post Reply
Author
mariusk View Drop Down
Newbie
Newbie


Joined: 23-Jun-2013
Posts: 2
Post Options Post Options   Quote mariusk Quote  Post ReplyReply Direct Link To This Post Topic: Validation Problem
    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.


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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down