ServerSide Validation through validation object
This commit is contained in:
26
Vidly/Models/Min18YearsIfAMember.cs
Normal file
26
Vidly/Models/Min18YearsIfAMember.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
|
||||
namespace Vidly.Models
|
||||
{
|
||||
public class Min18YearsIfAMember : ValidationAttribute
|
||||
{
|
||||
|
||||
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
|
||||
{
|
||||
var customer = (Customer)validationContext.ObjectInstance;
|
||||
if (customer.MembershipTypeId == MembershipType.Unknown ||
|
||||
customer.MembershipTypeId == MembershipType.PayAsYouGo)
|
||||
return ValidationResult.Success;
|
||||
if (customer.BirthDate == null)
|
||||
return new ValidationResult("Birthdate is required");
|
||||
var age = DateTime.Today.Year - customer.BirthDate.Value.Year;
|
||||
return (age > 18)
|
||||
? ValidationResult.Success
|
||||
: new ValidationResult("Customer should be at least 18 years old to go on a membership.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user