unobtrusive-javascript

Disable required validation by JavaScript

↘锁芯ラ 提交于 2019-11-28 03:20:34
问题 I have a create form to create an object. The create model has some properties that are only visible (.hide, .show()) if a checkbox is checked and that are marked required (by Attribute in Model). Unfortunatly when the checkbox is not checked, the required validation is performed on the properties hidden. How can I disable the required validation for this properties? I tried setting the data-val property of the input element to false but this does not work. Some an idea? Thanks in advance

First html helper generates client-side validation attributes, while the second one doesn't

喜夏-厌秋 提交于 2019-11-28 01:56:13
Let's say I have this model: public class Person { public bool IsApproved { get; set; } } And whis this codes, I am trying to render input with check type: @Html.CheckBoxFor(x => x.IsApproved) @Html.CheckBox("IsApproved") But, the results are different: // CheckBoxFor result <input data-val="true" data-val-required="The IsApproved field is required." id="IsApproved" name="IsApproved" type="checkbox" value="true"> <input name="IsApproved" type="hidden" value="false"> // CheckBox result <input id="IsApproved" name="IsApproved" type="checkbox" value="true"> <input name="IsApproved" type="hidden"

JavaScript: Access own Object Property inside Array Literal

人走茶凉 提交于 2019-11-27 23:14:44
Given an Array Literal inside a JavaScript Object, accessing its own object's properties does not seem to work: var closure = { myPic : document.getElementById('pic1'), picArray: [this.myPic] } alert(closure.picArray[0]); // alerts [undefined] Whereas declaring an Array Item by accessing an other JavaScript Object seem to work ​var closure1 = { ​ ​ myPic : document.getElementById('pic1') ​} ​ ​var closure2 = { ​ ​ picArray: [closure1.myPic] ​} ​ ​alert(closure2.picArray[0]); // alerts [object HTMLDivElement] Example: http://jsfiddle.net/5pmDG/ CMS The this value will not work like that, it

jQuery UI Dialog instead of alert() for Rails 3 data-confirm attribute

跟風遠走 提交于 2019-11-27 20:02:38
问题 In Rails 3, passing a :confirm parameter to link_to will populate the data-confirm attribute of the link. This will induce a JS alert() when the link is clicked. I am using the rails jQuery UJS adapter (https://github.com/rails/jquery-ujs). The relevant code from rails.js is: $('body').delegate('a[data-confirm], button[data-confirm], input[data-confirm]', 'click.rails', function () { var el = $(this); if (el.triggerAndReturn('confirm')) { if (!confirm(el.attr('data-confirm'))) { return false;

First html helper generates client-side validation attributes, while the second one doesn't

故事扮演 提交于 2019-11-27 19:12:36
问题 Let's say I have this model: public class Person { public bool IsApproved { get; set; } } And whis this codes, I am trying to render input with check type: @Html.CheckBoxFor(x => x.IsApproved) @Html.CheckBox("IsApproved") But, the results are different: // CheckBoxFor result <input data-val="true" data-val-required="The IsApproved field is required." id="IsApproved" name="IsApproved" type="checkbox" value="true"> <input name="IsApproved" type="hidden" value="false"> // CheckBox result <input

RequiredAttribute with AllowEmptyString=true in ASP.NET MVC 3 unobtrusive validation

霸气de小男生 提交于 2019-11-27 18:34:41
If i have [Required(AllowEmptyStrings = true)] declaration in my view model the validation is always triggered on empty inputs. I found the article which explains why it happens. Do you know if there is a fix available? If not, how do you handle it? Jon Galloway Note: I'm assuming you have AllowEmptyStrings = true because you're also using your view model outside of a web scenario; otherwise it doesn't seem like there's much of a point to having a Required attribute that allows empty strings in a web scenario. There are three steps to handle this: Create a custom attribute adapter which adds

Help, “this” is confusing me in JavaScript

一曲冷凌霜 提交于 2019-11-27 17:44:26
Working with the JavaScript one of the confusing thing is when using this var x = { ele : 'test', init : function(){ alert(this.ele); } } However when dealing with multiple object and especially events context of this changes and becomes confusing to keep track/understand. So if anybody has better inputs/guidelines/thoughts/better practices, please share. Also I would like know if using this gives any (performance) advantage or what? AnthonyWJones It's not about performance, it's about accessing a property of a specific instance of an object:- x.init() Would not display 'test' if you hadn't

Find user location using jQuery/JS (without google geolocation api)?

你说的曾经没有我的故事 提交于 2019-11-27 17:31:58
Is there a way to find a clients location on a website using just jQuery. For example, if a user comes to my site, how could I find out what their approximate location is just using jQuery. For example, if a user came from San Francisco CA it would have some type identifier to let me know the user came from San Francisco CA. I wouldn't really need their exact location just the county or general area of origin. edit: How is the information on http://flourishworks-webutils.appspot.com/req generated? Thanks To get the client IP address & country name in jQuery: $.getJSON("http://freegeoip.net

Javascript: Module Pattern vs Constructor/Prototype pattern?

时间秒杀一切 提交于 2019-11-27 17:04:35
I would like to know if the module pattern or Constructor/protoType pattern is more applicable to my work. Basically I am using unobtrusive javascript -- the HTML document has a reference to the .js file. My understanding of the module pattern: call an INIT method (which is basically a public method i can create and return using the module pattern) In the INIT method, assign all click events etc. This sounds like the perfect pattern for my situation, as I don't need to create Objects and inheritance hierarchies etc. My understanding of the Constructor/Prototype pattern: for creating objects

MVC unobtrusive validation on checkbox not working

北城以北 提交于 2019-11-27 17:03:46
I'm trying to implement the code as mentioned in this post . In other words I'm trying to implement unobtrusive validation on a terms and conditions checkbox. If the user hasn't selected the checkbox, then the input should be marked as invalid. This is the server side Validator code, I've added: /// <summary> /// Validation attribute that demands that a boolean value must be true. /// </summary> [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public class MustBeTrueAttribute : ValidationAttribute { public override bool IsValid(object value) { return value