This project has moved and is read-only. For the latest updates, please go here.

Costume Validation for the Database-First Approach

Jun 6, 2014 at 10:09 AM

How can I implement the CustomValidation for the 'Database-First' approach, like the one has been done for PublishDate in the 'Code-First' approach in the BookLibrary application?

I need to validate an input date against a list of calculated date intervals like (02/04/2010 - 04/06/2011, 07/08/2012 - 03/09/2012, ...)

I have written the following method for my validation and would like to use it for my custom validation:
        public static ValidationResult ValidateTransDate(DateTime transDate, int fk_DateInterval_ID)
            PropertyManagerEntities entitiesContext = new PropertyManagerEntities();

            IEnumerable<tblDateInterval> validDates =
                   (from d in entitiesContext.tblDateIntervals
                    where (d => d.DateInterval_ID == fk_DateInterval_ID))
                    select d).ToList();

            bool isValidDate = false;
            foreach (tblDateInterval dateRange in validDates)
                if (transDate >= dateRange.StartDate && transDate <= dateRange.EndDate) { isValidDate = true; break; }

            if (!isValidDate)
                return  new ValidationResult(string.Format(CultureInfo.CurrentCulture, Resources.DateOutOfRange, value, validDates));
            return ValidationResult.Success;
Your help is greatly appreciated.

Jun 8, 2014 at 7:24 PM
Hi Sabba,

CustomValidation is a feature of the .NET Validation infrastructure. It requires the [CustomValidation] attribute at the property. The attribute specifies the method to call.
[CustomValidation(typeof(Book), "ValidatePublishDate")]
public DateTime PublishDate
This method must match the expected method signature. The second parameter int fk_DateInterval_ID in your method is not allowed. You should see an exception with the following message:
An exception of type 'System.InvalidOperationException' occurred in System.ComponentModel.DataAnnotations.dll but was not handled in user code

Additional information: The CustomValidationAttribute method 'ValidatePublishDate' in type 'Book' must match the expected signature: public static ValidationResult ValidatePublishDate(object value, ValidationContext context).  The value can be strongly typed.  The ValidationContext parameter is optional. 
If you are using the EntityFramework database first approach then the designer creates the entity class for you and you cannot apply the CustomValidation attribute directly. The older WAF version 2.5 uses the database first approach as well. The BookLibrary application shows how it works. Here is an extract of the documentation describing this approach:

Validation with DataAnnotations

The .NET Framework ships a validation framework in the System.ComponentModel.DataAnnotations namespace.
But WPF Binding supports only validation with the IDataErrorInfo interface.
{Binding ... ValidatesOnDataErrors=true}
WAF provides the DataErrorInfoSupport class which connects the DataAnnotations validation with the IDataErrorInfo interface.

BookLibrary.Library.Domain/Person.cs (see Error and this[] property)

Because entity classes are partial classes generated by the Entity Framework, it's not possible to write the validation attributes directly to the properties.
A workaround is to introduce an interface with these properties and specify the attributes there.

BookLibrary.Library.Domain/Person.cs (see IPerson interface)

The MetadataType attribute defines the association between the entity class and the interface.

BookLibrary.Library.Domain/Person.cs (see Person class)

Enable the DataAnnotations infrastructure to use the MetadataType information.

BookLibrary.Library.Domain/BookLibraryEntities.cs (see static constructor)
Jun 9, 2014 at 4:13 PM
Hi jbe2277

Many thanks for your prompt and concise response.

I have applied your suggestions and now all my Grid and DataGrid controls (with the exception of ComboBoxes) are validated correctly in that the failed controls get red boundaries and command buttons become disabled.

I am now working on the ComboBox issues and hope to find a fix for them. They get validated correctly and prevent the user to make modifications to the other fields but their boundaries stay the same and the command buttons remain enabled.

Thanks again.