Print Page | Close Window

Validation Upon Save

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=2548
Printed Date: 04-Feb-2026 at 2:29pm


Topic: Validation Upon Save
Posted By: jipock
Subject: Validation Upon Save
Date Posted: 08-Mar-2011 at 10:20am
Good Afternoon,
 
I'm attempting now to validate an entity upon save that is Property1 on a specific entity type is null then stop the save and throw an exception.
 
I'm not finding a good way to do this... I thought to look into the EntityServerSaveInterceptor, but ValidateSave() doesn't take parameters, nor do I find a way of grabbing the entities being saved (and manually loop through all of them). The entity itself does have PropertyInterceptors for individual properties (before Set, After Set, etc.), but I don't want to check it until Save time.
 
Any suggestions on this?



Replies:
Posted By: sbelini
Date Posted: 08-Mar-2011 at 4:47pm
Hi jipock,
 
Assuming that the entity type you are looking for is Employee, the Property is LastName, and you are validating only new entities:
 
protected override bool ValidateSave() {
  var em = EntityManager;
  var foundEntities = em.FindEntities(typeof(Employee), EntityState.Added);
 
  foreach (Employee entity in foundEntities.Cast<Object>().ToList()) {
  if (entity.LastName == null) {
    em.RemoveEntity(entity);
  }
  return base.ValidateSave();
}
 
Note that, back in the client, while the successfully saved entities were updated to EntityState.Unchanged, the "unsaved" entities still have EntityState.Added.
 
Silvio.


Posted By: jipock
Date Posted: 09-Mar-2011 at 5:40am
Much thanks,  Silvio.



Print Page | Close Window