diff --git a/Vidly/Content/Site.css b/Vidly/Content/Site.css
index 9fbb2ba..0815e72 100644
--- a/Vidly/Content/Site.css
+++ b/Vidly/Content/Site.css
@@ -22,3 +22,9 @@ select,
textarea {
max-width: 280px;
}
+.field-validation-error{
+ color:red;
+}
+.input-validation-error{
+ border: 2px solid red;
+}
\ No newline at end of file
diff --git a/Vidly/Controllers/CustomersController.cs b/Vidly/Controllers/CustomersController.cs
index 85cb5ac..2496739 100644
--- a/Vidly/Controllers/CustomersController.cs
+++ b/Vidly/Controllers/CustomersController.cs
@@ -43,7 +43,17 @@ namespace Vidly.Controllers
[HttpPost]
public ActionResult Save(Customer customer)
{
- if(customer.Id==0)
+ if (!ModelState.IsValid)
+ {
+ var viewModel = new CustomerFormViewModel
+ {
+ Customer = customer,
+ MembershipTypes = _context.MembershipTypes.ToList()
+ };
+ return View("CustomerForm",viewModel);
+ }
+
+ if (customer.Id==0)
_context.Customers.Add(customer);
else
{
diff --git a/Vidly/Models/Customer.cs b/Vidly/Models/Customer.cs
index 56dd7de..c6ce668 100644
--- a/Vidly/Models/Customer.cs
+++ b/Vidly/Models/Customer.cs
@@ -10,11 +10,12 @@ namespace Vidly.Models
{
public int Id { get; set; }
- [Required]
+ [Required(ErrorMessage ="Please enter customer's name.")]
[StringLength(255)]
public string Name { get; set; }
[Display(Name = "Date of Birth")]
+ [Min18YearsIfAMember]
public DateTime? BirthDate { get; set; }
public bool IsSubscribedToNewsLetter { get; set; }
diff --git a/Vidly/Models/MembershipType.cs b/Vidly/Models/MembershipType.cs
index 232659d..5992235 100644
--- a/Vidly/Models/MembershipType.cs
+++ b/Vidly/Models/MembershipType.cs
@@ -14,5 +14,8 @@ namespace Vidly.Models
public short SignUpFee { get; set; }
public byte DurationInMonth { get; set; }
public byte DiscountRate { get; set; }
+
+ public static readonly byte Unknown = 0;
+ public static readonly byte PayAsYouGo = 1;
}
}
\ No newline at end of file
diff --git a/Vidly/Models/Min18YearsIfAMember.cs b/Vidly/Models/Min18YearsIfAMember.cs
new file mode 100644
index 0000000..ee813de
--- /dev/null
+++ b/Vidly/Models/Min18YearsIfAMember.cs
@@ -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.");
+ }
+ }
+}
\ No newline at end of file
diff --git a/Vidly/Vidly.csproj b/Vidly/Vidly.csproj
index 38ea25d..cb5cf07 100644
--- a/Vidly/Vidly.csproj
+++ b/Vidly/Vidly.csproj
@@ -247,6 +247,7 @@