ServerSide Validation through validation object
This commit is contained in:
@ -22,3 +22,9 @@ select,
|
|||||||
textarea {
|
textarea {
|
||||||
max-width: 280px;
|
max-width: 280px;
|
||||||
}
|
}
|
||||||
|
.field-validation-error{
|
||||||
|
color:red;
|
||||||
|
}
|
||||||
|
.input-validation-error{
|
||||||
|
border: 2px solid red;
|
||||||
|
}
|
||||||
@ -43,6 +43,16 @@ namespace Vidly.Controllers
|
|||||||
[HttpPost]
|
[HttpPost]
|
||||||
public ActionResult Save(Customer customer)
|
public ActionResult Save(Customer customer)
|
||||||
{
|
{
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
var viewModel = new CustomerFormViewModel
|
||||||
|
{
|
||||||
|
Customer = customer,
|
||||||
|
MembershipTypes = _context.MembershipTypes.ToList()
|
||||||
|
};
|
||||||
|
return View("CustomerForm",viewModel);
|
||||||
|
}
|
||||||
|
|
||||||
if (customer.Id==0)
|
if (customer.Id==0)
|
||||||
_context.Customers.Add(customer);
|
_context.Customers.Add(customer);
|
||||||
else
|
else
|
||||||
|
|||||||
@ -10,11 +10,12 @@ namespace Vidly.Models
|
|||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required(ErrorMessage ="Please enter customer's name.")]
|
||||||
[StringLength(255)]
|
[StringLength(255)]
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
[Display(Name = "Date of Birth")]
|
[Display(Name = "Date of Birth")]
|
||||||
|
[Min18YearsIfAMember]
|
||||||
public DateTime? BirthDate { get; set; }
|
public DateTime? BirthDate { get; set; }
|
||||||
|
|
||||||
public bool IsSubscribedToNewsLetter { get; set; }
|
public bool IsSubscribedToNewsLetter { get; set; }
|
||||||
|
|||||||
@ -14,5 +14,8 @@ namespace Vidly.Models
|
|||||||
public short SignUpFee { get; set; }
|
public short SignUpFee { get; set; }
|
||||||
public byte DurationInMonth { get; set; }
|
public byte DurationInMonth { get; set; }
|
||||||
public byte DiscountRate { get; set; }
|
public byte DiscountRate { get; set; }
|
||||||
|
|
||||||
|
public static readonly byte Unknown = 0;
|
||||||
|
public static readonly byte PayAsYouGo = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -247,6 +247,7 @@
|
|||||||
<Compile Include="Models\IdentityModels.cs" />
|
<Compile Include="Models\IdentityModels.cs" />
|
||||||
<Compile Include="Models\ManageViewModels.cs" />
|
<Compile Include="Models\ManageViewModels.cs" />
|
||||||
<Compile Include="Models\MembershipType.cs" />
|
<Compile Include="Models\MembershipType.cs" />
|
||||||
|
<Compile Include="Models\Min18YearsIfAMember.cs" />
|
||||||
<Compile Include="Models\Movie.cs" />
|
<Compile Include="Models\Movie.cs" />
|
||||||
<Compile Include="Models\MovieGenre.cs" />
|
<Compile Include="Models\MovieGenre.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|||||||
@ -17,17 +17,21 @@ else
|
|||||||
|
|
||||||
@using (@Html.BeginForm("Save", "Customers"))
|
@using (@Html.BeginForm("Save", "Customers"))
|
||||||
{
|
{
|
||||||
|
@Html.ValidationSummary(true, "Please fix the following errors.")
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(m => m.Customer.Name)
|
@Html.LabelFor(m => m.Customer.Name)
|
||||||
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
|
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
|
||||||
</div>
|
@Html.ValidationMessageFor(m => m.Customer.Name)
|
||||||
<div class="form-group">
|
|
||||||
@Html.LabelFor(m => m.Customer.BirthDate)
|
|
||||||
@Html.TextBoxFor(m => m.Customer.BirthDate, "{0:d MMM yyyy}", new { @class = "form-control" })
|
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(m => m.Customer.MembershipTypeId)
|
@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.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>
|
||||||
<div class="checkbox">
|
<div class="checkbox">
|
||||||
<label>
|
<label>
|
||||||
|
|||||||
Reference in New Issue
Block a user