I'm trying to check to see if a control (or preferably, the entire form, but it doesn't seem like this is possible) is valid, but I'm always getting a "true" result even if the control shouldn't validate.
MVC Helper:
http://pastebin.com/cgj6pDcn
Then, I create a view for a IG textbox for a property "Qty" with the following annotations:
[Required]
[DataType("Int")]
[Range(12, 13)]
[Display(Name = "Qty")]
public int Qty { get; set; }
BLOCKED SCRIPT
var isValid = ($('#Qty').igValidator('isValidState'));
isValid is always true, no matter what's in the Qty textbox. What am I doing wrong?
Hi Josh,
If you use range, then I think, there is no need for any validation checks on client, because value in editor should be always valid (well, until end user will find a way to submit without leaving focus while editing). I assume that you use NumericEditorFor or CurrencyEditorFor or similar for your Qty field in model, othewise, range validation will have no effect or it will have unpredictable results. The range is converted by Mvc server to min/maxValue options. If initial value is not in range, then editor will set closest value: in your particular example it will be 12. When end user will enter invalid number, then error message should appear and if end user leaves focus, then editor should adjust its value to min/max limit automatically. Application may check if value is valid, but it may return false only while editor has invalid value and focus at the same time. I wrote for you example, which does validation onmouseover a button. To test, you may enter wrong value and move mouse over button.
Note: you should not use something likevar isValid = $('#Qty').igValidator('isValidState');because it may work only when INPUT of igEditor has the id=Qty. That will fail if editor has buttons, because that id is applied to outer SPAN and INPUT has no id at all.You should get reference to igValidator directly from igEditor.
<%: Html.Infragistics().NumericEditorFor(m => m.Qty). // example to enable spin buttons //ButtonType(TextEditorButtonType.Spin). // example to customize igValidator on server //ValidatorOptions(vo => vo.KeepFocus(ValidatorKeepFocus.Always)). Render() %><script type="text/javascript"> function validateQty() { var vld = $('#Qty').igEditor('validator'); var isValid = vld ? vld.isValidState() : '?'; alert('isValid:' + isValid); }</script><input type="button" value="check" onmouseover="validateQty()" />
Ok, I got to the sample at http://samples.infragistics.com/jquery/editors/editors-validation (replaced "local" with "com").
However, it doesn't really help with my issue. The sample doesn't use C# data annotations or the "isValidState" method, it instead sets the validation rules using the "ValidatorOptions" method, which cause validation checking on blur, change, and submit. These work as expected, but can't be used in my case.
I need to check validation during a partial postback, so the form submit event isn't fired. This is why I'd like to manually check if the control is in a valid state per it's associated C# property's data annotations, presumably checking the "isValidState" method.
Will the "IsValidState" method return false if the control is invalid? If so, is the control considered invalid if it's value doesn't match the C# data annotation for it's associated property?
In the example I provided, even though my "Qty" property has a "Required" annotation, it's "IsValidState" method always returns true, even if the field is blank.
Thanks Tsvetelina. However, the online sample link you provided is broken.
Also, if I've included the entire infragistics.js file, do I still need to use the loader?
Hello,
I tried your approach using our online sample:
http://samples.infragistics.local/jquery/editors/editors-validation
I worked like expected.
I am not able to see your view from the code snippet.
Are you sure that you have included the needed js and css files?
http://help.infragistics.com/NetAdvantage/jquery/2012.1?page=Infragistics.Web.Mvc~Infragistics.Web.Mvc.NumericEditorModel~ValidatorOptions.html
$.ig.loader({
scriptPath: '/samplesbrowser/samplescommon/jquery/common/js/',
cssPath: '/samplesbrowser/samplescommon/jquery/common/css/',
resources: 'igEditors,igValidator,igShared'
});
Hope this helps