From 3e501b29d0da2fe56c90e5eb38f4add461310f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Wed, 23 Jan 2019 21:43:27 +0100 Subject: [PATCH] ServerSide Validation through validation object --- Vidly/Content/Site.css | 6 ++++++ Vidly/Controllers/CustomersController.cs | 12 ++++++++++- Vidly/Models/Customer.cs | 3 ++- Vidly/Models/MembershipType.cs | 3 +++ Vidly/Models/Min18YearsIfAMember.cs | 26 +++++++++++++++++++++++ Vidly/Vidly.csproj | 1 + Vidly/Views/Customers/CustomerForm.cshtml | 14 +++++++----- 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 Vidly/Models/Min18YearsIfAMember.cs 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 @@ + diff --git a/Vidly/Views/Customers/CustomerForm.cshtml b/Vidly/Views/Customers/CustomerForm.cshtml index cbf3994..3765a25 100644 --- a/Vidly/Views/Customers/CustomerForm.cshtml +++ b/Vidly/Views/Customers/CustomerForm.cshtml @@ -6,7 +6,7 @@ Layout = "~/Views/Shared/_Layout.cshtml"; } -@if (Model.Customer.Id==null|| Model.Customer.Id == 0) +@if (Model.Customer.Id == null || Model.Customer.Id == 0) {

New Customer

} @@ -17,17 +17,21 @@ else @using (@Html.BeginForm("Save", "Customers")) { + @Html.ValidationSummary(true, "Please fix the following errors.")
@Html.LabelFor(m => m.Customer.Name) @Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" }) -
-
- @Html.LabelFor(m => m.Customer.BirthDate) - @Html.TextBoxFor(m => m.Customer.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" }) + @Html.ValidationMessageFor(m => m.Customer.Name)
@Html.LabelFor(m => m.Customer.MembershipTypeId) @Html.DropDownListFor(m => m.Customer.MembershipTypeId, new SelectList(Model.MembershipTypes, "Id", "Name"), "Select membership Type", new { @class = "form-control" }) + @Html.ValidationMessageFor(m => m.Customer.MembershipTypeId) +
+
+ @Html.LabelFor(m => m.Customer.BirthDate) + @Html.TextBoxFor(m => m.Customer.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" }) + @Html.ValidationMessageFor(m => m.Customer.BirthDate)