Custom validation

The built-in validations exposed in the framework may be solving most of our technical validation problems, but there may be some business needs/requirements that require additional provision on top of built-in validations.

The custom validation can be performed at two levels:

  • Field level
  • Class level

The field-level custom validator can be created using the following:

  • Inherit the custom validation class from the ValidationAttribute base type
  • Override the IsValid method and handle your business validation
  • The IsValid method accepts two parameters—value and validationContext
  • The value field is the actual field value against which the attribute is configured
  • The validationContext field would have ObjectInstance of the model ...

Get Mastering Entity Framework Core 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.