ServerSide Validation through validation object

This commit is contained in:
2019-01-23 21:43:27 +01:00
parent ecadd21228
commit 3e501b29d0
7 changed files with 58 additions and 7 deletions

View File

@ -22,3 +22,9 @@ select,
textarea {
max-width: 280px;
}
.field-validation-error{
color:red;
}
.input-validation-error{
border: 2px solid red;
}

View File

@ -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
{

View File

@ -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; }

View File

@ -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;
}
}

View 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.");
}
}
}

View File

@ -247,6 +247,7 @@
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="Models\ManageViewModels.cs" />
<Compile Include="Models\MembershipType.cs" />
<Compile Include="Models\Min18YearsIfAMember.cs" />
<Compile Include="Models\Movie.cs" />
<Compile Include="Models\MovieGenre.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -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)
{
<h2>New Customer</h2>
}
@ -17,17 +17,21 @@ else
@using (@Html.BeginForm("Save", "Customers"))
{
@Html.ValidationSummary(true, "Please fix the following errors.")
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
</div>
<div class="form-group">
@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)
</div>
<div class="form-group">
@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)
</div>
<div class="form-group">
@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)
</div>
<div class="checkbox">
<label>