From 9f0e5f7b1d948690f1bac377c4a4813c68f75ebd Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Sun, 3 Mar 2013 20:43:13 -0500 Subject: [PATCH] Validation for purpose select box working --- Web/Controllers/CreateLogController.cs | 10 +++++-- Web/DAL/DataService.cs | 5 ---- Web/Scripts/Shared/Site.js | 27 ++++++++++++------- .../CreateLog/CreateLogViewModel.cs | 4 +-- .../SelectListViewModel.cshtml | 2 +- Web/Views/Shared/_Layout.cshtml | 2 +- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Web/Controllers/CreateLogController.cs b/Web/Controllers/CreateLogController.cs index 4f47cfc..b66c7d3 100644 --- a/Web/Controllers/CreateLogController.cs +++ b/Web/Controllers/CreateLogController.cs @@ -59,11 +59,17 @@ namespace MileageTraker.Web.Controllers log.Source = HttpContext.Request.Url.AbsolutePath; log.UserHostAddress = HttpContext.Request.UserHostAddress; log.UserAgent = HttpContext.Request.UserAgent; + + log.Purpose = + DataService.GetPurposeTypes() + .First(pt => pt.PurposeTypeId == model.Purpose.Selected); + DataService.AddLog(log); TempData["StatusMessage-Type"] = "alert-success"; TempData["StatusMessage"] = @"You've successfully created an entry - traveling to " + model.CityName + @" + traveling to " + model.CityName + @" + for " + log.Purpose.Purpose + @" on " + model.Date.ToShortDateString() + @" in Vehicle Id " + model.VehicleId + @" ending in " + model.EndOdometer + @" @@ -106,7 +112,7 @@ namespace MileageTraker.Web.Controllers private SelectList GetPurposeTypesSelectList() { - var selectList = new SelectList(DataService.GetPurposeTypesWithBlank(), "PurposeTypeId", "Purpose"); + var selectList = new SelectList(DataService.GetPurposeTypes(), "PurposeTypeId", "Purpose"); return selectList; } } diff --git a/Web/DAL/DataService.cs b/Web/DAL/DataService.cs index 514b277..6e68442 100644 --- a/Web/DAL/DataService.cs +++ b/Web/DAL/DataService.cs @@ -330,11 +330,6 @@ namespace MileageTraker.Web.DAL select pt).OrderBy(pt => pt.SortOrder).ToList(); } - public IEnumerable GetPurposeTypesWithBlank() - { - return (new[] { new PurposeType() }).Concat(GetPurposeTypes()); - } - #endregion #region Vehicle diff --git a/Web/Scripts/Shared/Site.js b/Web/Scripts/Shared/Site.js index e119340..bfd35db 100644 --- a/Web/Scripts/Shared/Site.js +++ b/Web/Scripts/Shared/Site.js @@ -187,16 +187,17 @@ $(function() { * The errors are produced by the MVC unobtrusive validation. */ $(function () { + var errorSelector = 'span.field-validation-error, .input-validation-error'; $('form').submit(function () { $(this).find('div.control-group').each(function () { - if ($(this).find('span.field-validation-error').length == 0) { + if ($(this).find(errorSelector).length == 0) { $(this).removeClass('error'); } }); if (!$(this).valid()) { $(this).find('div.control-group').each(function () { - if ($(this).find('span.field-validation-error').length > 0) { + if ($(this).find(errorSelector).length > 0) { $(this).addClass('error'); } }); @@ -204,7 +205,7 @@ $(function () { }); $('form').each(function () { $(this).find('div.control-group').each(function () { - if ($(this).find('span.field-validation-error').length > 0) { + if ($(this).find(errorSelector).length > 0) { $(this).addClass('error'); } }); @@ -213,14 +214,22 @@ $(function () { //Update that validator if ($.validator === undefined) return; + $.validator.setDefaults({ - highlight: function (element) { - $(element).closest(".control-group").addClass("error"); + highlight: function(element) { + if (element.type === 'radio') { + this.findByName(element.name).closest(".control-group").addClass("error"); + } else { + $(element).closest(".control-group").addClass("error"); + } }, - unhighlight: function (element) { - $(element).closest(".control-group").removeClass("error"); - }, - debug: true + unhighlight: function(element) { + if (element.type === 'radio') { + this.findByName(element.name).closest(".control-group").removeClass("error"); + } else { + $(element).closest(".control-group").removeClass("error"); + } + } }); }); diff --git a/Web/ViewModels/CreateLog/CreateLogViewModel.cs b/Web/ViewModels/CreateLog/CreateLogViewModel.cs index 85b1f3a..96ad2fc 100644 --- a/Web/ViewModels/CreateLog/CreateLogViewModel.cs +++ b/Web/ViewModels/CreateLog/CreateLogViewModel.cs @@ -39,10 +39,10 @@ namespace MileageTraker.Web.ViewModels.CreateLog [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] public string CityName { get; set; } - [Required(ErrorMessage = "Required")] [Display(Name = "Trip Purpose")] + [Required] public SelectListViewModel Purpose { get; set; } - + [InputSize("large")] public string Notes { get; set; } diff --git a/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml b/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml index 7c24168..79d0764 100644 --- a/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml +++ b/Web/Views/Shared/EditorTemplates/SelectListViewModel.cshtml @@ -3,4 +3,4 @@ Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml"; ViewData.ModelMetadata.DisplayName = "Purpose"; } -@Html.DropDownListFor(m => m.Selected, Model.Available) \ No newline at end of file +@Html.DropDownListFor(m => m.Selected, Model.Available, string.Empty) \ No newline at end of file diff --git a/Web/Views/Shared/_Layout.cshtml b/Web/Views/Shared/_Layout.cshtml index 94510a5..5c23632 100644 --- a/Web/Views/Shared/_Layout.cshtml +++ b/Web/Views/Shared/_Layout.cshtml @@ -54,9 +54,9 @@ - + @RenderSection("Scripts", false)