unobtrusive-validation

Adding jQuery validator rules to dynamically created elements in ASP

天大地大妈咪最大 提交于 2019-11-27 11:35:06
I've got some dynamically inserted form fields on a page in an MVC3 project. Normally we would add the jQuery validation server-side, but in this case we can't (multiple fields in the UI generate the value for one hidden field - and this is what is submitted. We can't validate against a hidden field, so we must instead add UI-only validation for the fields the user can see) Once the fields are dynamically added to the page, I run the following code over the container: $container.find(".date").rules("add", { required: true, messages: { required: "The date is required" } }); But it doesn't work!

Best Practices ViewModel Validation in ASP.NET MVC

谁都会走 提交于 2019-11-27 10:52:48
I am using DataAnnotations to validate my ViewModel on client side with jquery.validate.unobtrusive and on server side in ASP.NET MVC application. Not so long time ago, I figured out that I can write validation like this: [Required(ErrorMessage = "{0} is required")] public string Name { get; set; } That way I can easily define some general strings in config or in resources and always use it in DataAnnotations . So it will be easier to change validation messages in my whole application in future. Also I know that there is a FluentValidation library that allows to add validation rules to already

ASP.NET MVC 3: Required steps for unobtrusive client-side validation of dynamic/AJAX content

爷,独闯天下 提交于 2019-11-27 10:19:59
What are the complete set of Steps Required for client-side unobtrusive validation to work for dynamically inserted form fields? Relevant SO posts ASP.NET MVC 3 unobtrusive client-side validation with dynamic content - He needed the unobtrusive validation attributes to show up in the generated HTML and did so by calling BeginForm ASP.Net MVC 3 validation on AjaxForm - The asker was using Ajax.BeginForm which uses MicrosoftAjax instead of JQuery.validation. PartialView and unobtrusive client validation not working - He had the problem with unobtrusive validation attributes not showing up and

How to fire jQuery function only if form is valid

不问归期 提交于 2019-11-27 07:32:39
I have a jQuery function tied to my submit button like this: $(function () { $('#signupform').submit(function () { alert('test'); }); }); However, it fires whether or not the form is valid. My model is decorated with various DataAnnotations and the client-side validation is working well, but I only want that jQuery function to fire if the form has been validated. How do I accomplish that? EDIT: To clarify, I'm using MVC DataAnnotations + jQuery's unobtrusive javascript to handle the client-side validation. I do not have my own javascript validation routines written. The built in jQuery

Validating time-only input in asp.net MVC unobtrusive validation

旧时模样 提交于 2019-11-27 06:38:59
问题 I have two separate fields on the page: one for date and one for time. This is the model: [Required] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")] public DateTime? StartTime { get; set; } [Required] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public DateTime Date { get; set; } This is the view: @Html.TextBoxFor(m => m.Date, "{0:MM/dd/yyyy}", new { type = "text" }) @Html.TextBoxFor(m => m.StartTime, "{0:hh:mm tt}", new {

MVC unobtrusive range validation of dynamic values

二次信任 提交于 2019-11-27 04:22:53
问题 I have a value on my model, that must fall within the range of two other values on my model. For example: public class RangeValidationSampleModel { int Value { get; set; } int MinValue { get; set; } int MaxValue { get; set; } } Of course, I can't pass these Min/MaxValues into my DataAnnotations attributes, as they have to be constant values. I'm sure I need to build my own validation attribute, but I haven't done this much and can't wrap my mind around how it should work. I've searched for

mvc clientside validation for nested (collection) properties

一曲冷凌霜 提交于 2019-11-27 03:34:00
问题 I'm using asp.net mvc 3 with jquery unobtrusive validation. I recently changed from standard DataAnnotations to FluentValidation and it works great. My primary reason for picking up FluentValidation was the need to validate nested properties on my viewmodel (but i found there are other cool reasons for using it) that kinda looks like this (don't mind accessors this is pseudo): class Vm { string Prop; string AnotherProp; IEnumerable<ElementsVm> Elements; } class ElementsVm { bool Required;

Model inheritance possible when using strongly-typed view in MVC3?

不羁的心 提交于 2019-11-27 03:25:28
问题 I have the following setup in my model: namespace QuickTest.Models { public class Person { [Required] [Display(Name = "Full name")] public string FullName { get; set; } [Display(Name = "Address Line 1")] public virtual string Address1 { get; set; } } public class Sender : Person { [Required] public override string Address1 { get; set; } } public class Receiver : Person { } } and in my view: @model QuickTest.Models.Person @{ ViewBag.Title = "Edit"; } <script src="@Url.Content("~/Scripts/jquery

MVC3 Validation - Require One From Group

白昼怎懂夜的黑 提交于 2019-11-27 02:55:39
Given the following viewmodel: public class SomeViewModel { public bool IsA { get; set; } public bool IsB { get; set; } public bool IsC { get; set; } //... other properties } I wish to create a custom attribute that validates at least one of the available properties are true. I envision being able to attach an attribute to a property and assign a group name like so: public class SomeViewModel { [RequireAtLeastOneOfGroup("Group1")] public bool IsA { get; set; } [RequireAtLeastOneOfGroup("Group1")] public bool IsB { get; set; } [RequireAtLeastOneOfGroup("Group1")] public bool IsC { get; set; } /

ASP.NET MVC 3: Generate unobtrusive validation when BeginForm is on the layout

蹲街弑〆低调 提交于 2019-11-27 02:23:59
问题 I just realized that when I place a form tag on my layout page, surrounding the RenderBody section, the unobtrusive validation is not being generated. Something like this: @using (Html.BeginForm()) { <input type="submit" value="save" /> <div> @RenderBody() </div> } As you might have guessed I want to generate buttons over my content. Is this the correct unobtrusive's behavior? BTW, If I place the form inside a particular page, everything works like a charm: the data-val* attributes are well