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.Source = HttpContext.Request.Url.AbsolutePath;
|
||||||
log.UserHostAddress = HttpContext.Request.UserHostAddress;
|
log.UserHostAddress = HttpContext.Request.UserHostAddress;
|
||||||
log.UserAgent = HttpContext.Request.UserAgent;
|
log.UserAgent = HttpContext.Request.UserAgent;
|
||||||
|
|
||||||
|
log.Purpose =
|
||||||
|
DataService.GetPurposeTypes()
|
||||||
|
.First(pt => pt.PurposeTypeId == model.Purpose.Selected);
|
||||||
|
|
||||||
DataService.AddLog(log);
|
DataService.AddLog(log);
|
||||||
TempData["StatusMessage-Type"] = "alert-success";
|
TempData["StatusMessage-Type"] = "alert-success";
|
||||||
TempData["StatusMessage"] =
|
TempData["StatusMessage"] =
|
||||||
@"You've successfully created an entry
|
@"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>
|
on <strong>" + model.Date.ToShortDateString() + @"</strong>
|
||||||
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
|
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
|
||||||
ending in <strong>" + model.EndOdometer + @"</strong>
|
ending in <strong>" + model.EndOdometer + @"</strong>
|
||||||
@@ -106,7 +112,7 @@ namespace MileageTraker.Web.Controllers
|
|||||||
|
|
||||||
private SelectList GetPurposeTypesSelectList()
|
private SelectList GetPurposeTypesSelectList()
|
||||||
{
|
{
|
||||||
var selectList = new SelectList(DataService.GetPurposeTypesWithBlank(), "PurposeTypeId", "Purpose");
|
var selectList = new SelectList(DataService.GetPurposeTypes(), "PurposeTypeId", "Purpose");
|
||||||
return selectList;
|
return selectList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -330,11 +330,6 @@ namespace MileageTraker.Web.DAL
|
|||||||
select pt).OrderBy(pt => pt.SortOrder).ToList();
|
select pt).OrderBy(pt => pt.SortOrder).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<PurposeType> GetPurposeTypesWithBlank()
|
|
||||||
{
|
|
||||||
return (new[] { new PurposeType() }).Concat(GetPurposeTypes());
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Vehicle
|
#region Vehicle
|
||||||
|
|||||||
@@ -187,16 +187,17 @@ $(function() {
|
|||||||
* The errors are produced by the MVC unobtrusive validation.
|
* The errors are produced by the MVC unobtrusive validation.
|
||||||
*/
|
*/
|
||||||
$(function () {
|
$(function () {
|
||||||
|
var errorSelector = 'span.field-validation-error, .input-validation-error';
|
||||||
$('form').submit(function () {
|
$('form').submit(function () {
|
||||||
$(this).find('div.control-group').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).removeClass('error');
|
$(this).removeClass('error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!$(this).valid()) {
|
if (!$(this).valid()) {
|
||||||
$(this).find('div.control-group').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');
|
$(this).addClass('error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -204,7 +205,7 @@ $(function () {
|
|||||||
});
|
});
|
||||||
$('form').each(function () {
|
$('form').each(function () {
|
||||||
$(this).find('div.control-group').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');
|
$(this).addClass('error');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -213,14 +214,22 @@ $(function () {
|
|||||||
//Update that validator
|
//Update that validator
|
||||||
if ($.validator === undefined)
|
if ($.validator === undefined)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
$.validator.setDefaults({
|
$.validator.setDefaults({
|
||||||
highlight: function (element) {
|
highlight: function(element) {
|
||||||
$(element).closest(".control-group").addClass("error");
|
if (element.type === 'radio') {
|
||||||
|
this.findByName(element.name).closest(".control-group").addClass("error");
|
||||||
|
} else {
|
||||||
|
$(element).closest(".control-group").addClass("error");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
unhighlight: function (element) {
|
unhighlight: function(element) {
|
||||||
$(element).closest(".control-group").removeClass("error");
|
if (element.type === 'radio') {
|
||||||
},
|
this.findByName(element.name).closest(".control-group").removeClass("error");
|
||||||
debug: true
|
} else {
|
||||||
|
$(element).closest(".control-group").removeClass("error");
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -39,10 +39,10 @@ namespace MileageTraker.Web.ViewModels.CreateLog
|
|||||||
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
|
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
|
||||||
public string CityName { get; set; }
|
public string CityName { get; set; }
|
||||||
|
|
||||||
[Required(ErrorMessage = "Required")]
|
|
||||||
[Display(Name = "Trip Purpose")]
|
[Display(Name = "Trip Purpose")]
|
||||||
|
[Required]
|
||||||
public SelectListViewModel Purpose { get; set; }
|
public SelectListViewModel Purpose { get; set; }
|
||||||
|
|
||||||
[InputSize("large")]
|
[InputSize("large")]
|
||||||
public string Notes { get; set; }
|
public string Notes { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
|
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
|
||||||
ViewData.ModelMetadata.DisplayName = "Purpose";
|
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-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.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.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/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/Shared/Site.js")" type="text/javascript"></script>
|
||||||
|
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
|
||||||
@RenderSection("Scripts", false)
|
@RenderSection("Scripts", false)
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user