Fluent Metadata Custom Validation

Nov 8, 2012 at 9:46 AM

I'm trying to use Custom Validation with Fluent Metadata but it doesn't have any documentation or sample usage.

Can someone provide a sample for this?

Thanks,

 

Douglas

Developer
Nov 9, 2012 at 9:27 PM

Hi,

Suppose you have a class Person with a property FirstName, and a class TestPersonData with a method CheckFirstname like this:

 

public class PersonDataValidator
        {
            public static ValidationResult ValidateFirstName(string firstName)
            {
                if(string.IsNullOrEmpty(firstName))
                {
                    return new ValidationResult(
                        "Invalid first name.");
                }
                return ValidationResult.Success;
            }

        }

 

Now you can use the Fluent API to register this validation method for the property FirstName on the entity Person:

            metadataContainer.Entity<Person>().Validation(x => x.FirstName).Custom<PersonValidator>("ValidateFirstName");

I hope this helps you

Kind regards,

Merijn

Nov 9, 2012 at 9:52 PM

Hi Merijn.

I'll try it.

 

Thanks!