locked
How do I validate one field based on the data in the another in the same table in my Dynamic Data Website? RRS feed

  • Question

  • User943994099 posted

    I have a table in a my Dynamic Data website that has a Primary Phone Number field and a Country field. I have regular expression for the phone field that validate US domestic phone numbers and International phone numbers. How would I write in my dynamic data application that if a country other than the us is selected in the Country field then the international phone regular expression should be used?

    Monday, October 31, 2011 1:07 PM

Answers

  • User1155138559 posted

    you can override the on saving method of the entity and detect this case and throw the exception. look at "Customizing Validation for All Data Fields by Using a Partial-Class Method in the ADO.NET Entity Framework" in http://msdn.microsoft.com/en-us/library/cc488527.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2011 1:21 PM
  • User3866881 posted

    Hello mojarary2k:)

    Main two ways:

    1) Just use specific tags (called "Attributes") to range your number, ect. Something looks like this following:

     [Range("UnitsInStock",10,50,    ErrorMessage="Must be between {1} and {2}")]
    public partial class Product{} //Here Product should be a partial model class generated from LINQ or EntityFramework.

    2) You can also override the OnPropertyChanging event to implement your own validation:

     public partial class Product
        { 
              partial void OnUnitsInStockChanging(short? value)
                {    
                  if (value < 10 || value > 50)      
                     throw new  ValidationException("must be between 10 and 50");  
               }
       }

    See this: http://davidhayden.com/blog/dave/archive/2008/01/01/ASPNETDynamicDataValidationLINQToSQLOnPropertyChangingPartialMethods.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2011 11:00 PM

All replies

  • User1155138559 posted

    you can override the on saving method of the entity and detect this case and throw the exception. look at "Customizing Validation for All Data Fields by Using a Partial-Class Method in the ADO.NET Entity Framework" in http://msdn.microsoft.com/en-us/library/cc488527.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 31, 2011 1:21 PM
  • User3866881 posted

    Hello mojarary2k:)

    Main two ways:

    1) Just use specific tags (called "Attributes") to range your number, ect. Something looks like this following:

     [Range("UnitsInStock",10,50,    ErrorMessage="Must be between {1} and {2}")]
    public partial class Product{} //Here Product should be a partial model class generated from LINQ or EntityFramework.

    2) You can also override the OnPropertyChanging event to implement your own validation:

     public partial class Product
        { 
              partial void OnUnitsInStockChanging(short? value)
                {    
                  if (value < 10 || value > 50)      
                     throw new  ValidationException("must be between 10 and 50");  
               }
       }

    See this: http://davidhayden.com/blog/dave/archive/2008/01/01/ASPNETDynamicDataValidationLINQToSQLOnPropertyChangingPartialMethods.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 1, 2011 11:00 PM