locked
Compair date validation in dynamic data RRS feed

  • Question

  • User-28820329 posted

    Hi,

    I have a table with a start and a end date. I want to validate for end date is after start date.

    How do I do that with Dynamic data. I can not find something like a compair validation atribute.

    thank you!

     

    Rob

     

    Sunday, November 13, 2011 5:19 AM

Answers

  • User-330204900 posted
    namespace WebSite.Models
    {
        public partial class MyEntities
        {
            partial void OnContextCreated()
            {
                // Register the handler for the SavingChanges event. 
                this.SavingChanges += new EventHandler(context_SavingChanges);
            }
    
            private static void context_SavingChanges(object sender, EventArgs e)
            {
                var objects = ((ObjectContext)sender).ObjectStateManager;
    
                #region Inserted objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added))
                {
                    if (entry.Entity != null)
                    {
                        // auto update the 
                        if (entry.Entity.GetType() == typeof(TestEntity))
                        {
                            var testEntity = entry.Entity as TestEntity;
                            if (testEntity != null)
                            {
                                if (testEntity.StartDate > testEntity.EndDate)
                                    throw new ValidationException("StartDate must be before EndDate");
                            }
                        }
                    }
                }
                #endregion
    
                #region Updated objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Modified))
                {
                    if (entry.Entity != null)
                    {
                        // auto update the 
                        if (entry.Entity.GetType() == typeof(TestEntity))
                        {
                            var testEntity = entry.Entity as TestEntity;
                            if (testEntity != null)
                            {
                                if (testEntity.StartDate > testEntity.EndDate)
                                    throw new ValidationException("StartDate must be before EndDate");
                            }
                        }
                    }
                }
                #endregion
    
    
                #region Delete objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Deleted))
                {
                    if (entry.Entity != null)
                    {
                        // ================================================================================
                        //  NOTE: to self remember that during delete entities only have primary key value 
                        // ================================================================================
                    }
                }
                #endregion
            }
        }
    }

    You could optomise this by calling a single test method but that is up to you :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 13, 2011 10:29 AM

All replies

  • User-330204900 posted

    Hi Rob, I would do the validation in the data model and throw a ValidationException as doing this on the client is rather complex.

    Sunday, November 13, 2011 5:44 AM
  • User-28820329 posted

    Ok

    do you have an example how to do so?

    thanks

    Sunday, November 13, 2011 5:54 AM
  • User-330204900 posted

    What data model are you using for your site Linq to SQL or Entity Framework?

    Sunday, November 13, 2011 5:57 AM
  • User-28820329 posted

    Sorry sjnaughton,

    I was absent for a little time..

    I am using a ADO entity data model.

     

     

    Sunday, November 13, 2011 10:20 AM
  • User-330204900 posted
    namespace WebSite.Models
    {
        public partial class MyEntities
        {
            partial void OnContextCreated()
            {
                // Register the handler for the SavingChanges event. 
                this.SavingChanges += new EventHandler(context_SavingChanges);
            }
    
            private static void context_SavingChanges(object sender, EventArgs e)
            {
                var objects = ((ObjectContext)sender).ObjectStateManager;
    
                #region Inserted objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Added))
                {
                    if (entry.Entity != null)
                    {
                        // auto update the 
                        if (entry.Entity.GetType() == typeof(TestEntity))
                        {
                            var testEntity = entry.Entity as TestEntity;
                            if (testEntity != null)
                            {
                                if (testEntity.StartDate > testEntity.EndDate)
                                    throw new ValidationException("StartDate must be before EndDate");
                            }
                        }
                    }
                }
                #endregion
    
                #region Updated objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Modified))
                {
                    if (entry.Entity != null)
                    {
                        // auto update the 
                        if (entry.Entity.GetType() == typeof(TestEntity))
                        {
                            var testEntity = entry.Entity as TestEntity;
                            if (testEntity != null)
                            {
                                if (testEntity.StartDate > testEntity.EndDate)
                                    throw new ValidationException("StartDate must be before EndDate");
                            }
                        }
                    }
                }
                #endregion
    
    
                #region Delete objects
                foreach (ObjectStateEntry entry in objects.GetObjectStateEntries(EntityState.Deleted))
                {
                    if (entry.Entity != null)
                    {
                        // ================================================================================
                        //  NOTE: to self remember that during delete entities only have primary key value 
                        // ================================================================================
                    }
                }
                #endregion
            }
        }
    }

    You could optomise this by calling a single test method but that is up to you :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, November 13, 2011 10:29 AM
  • User-28820329 posted

    Sjnaughton,

    It works as advertised!

    thank you a lot!

     

    Rob

    Sunday, November 13, 2011 11:19 AM
  • User-330204900 posted

    your welcome

    Sunday, November 13, 2011 11:56 AM