Validation for purpose select box working
This commit is contained in:
@@ -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 <strong>" + model.CityName + @"</strong>
|
||||
traveling to <strong>" + model.CityName + @"</strong>
|
||||
for <strong>" + log.Purpose.Purpose + @"</strong>
|
||||
on <strong>" + model.Date.ToShortDateString() + @"</strong>
|
||||
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
|
||||
ending in <strong>" + model.EndOdometer + @"</strong>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,11 +330,6 @@ namespace MileageTraker.Web.DAL
|
||||
select pt).OrderBy(pt => pt.SortOrder).ToList();
|
||||
}
|
||||
|
||||
public IEnumerable<PurposeType> GetPurposeTypesWithBlank()
|
||||
{
|
||||
return (new[] { new PurposeType() }).Concat(GetPurposeTypes());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Vehicle
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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; }
|
||||
|
||||
|
||||
@@ -3,4 +3,4 @@
|
||||
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
|
||||
ViewData.ModelMetadata.DisplayName = "Purpose";
|
||||
}
|
||||
@Html.DropDownListFor(m => m.Selected, Model.Available)
|
||||
@Html.DropDownListFor(m => m.Selected, Model.Available, string.Empty)
|
||||
@@ -54,9 +54,9 @@
|
||||
<script src="@Url.Content("~/Scripts/jquery-ui-1.9.2.custom.min.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/jquery.numeric.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/jquery.qtip.min.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/Shared/Site.js")" type="text/javascript"></script>
|
||||
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
|
||||
@RenderSection("Scripts", false)
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user