c# - ASP.NET MVC Model validation ignoring inputs that have become readonly -
i have form shipping , billing information using view model containing required attributes.
here's exctract of code enough explain issue.
extract of view model:
public class checkoutviewmodel { [required] public string shippingpostalcode { get; set; } [required(errormessage = "*")] public string billingpostalcode { get; set; } }
extract of razor form:
@html.textboxfor(m => m.shippingpostalcode, new { @class = "form-control", placeholder = "postal code" }) @html.textboxfor(m => m.billingpostalcode, new { @class = "form-control", placeholder = "postal code", data_bind = "attr: { 'readonly': billingsameasshipping }" }) @html.validationmessagefor(model => model.billingpostalcode)
i use knockout make billing fields (including billingpostalcode) readonly if checkbox checked using observable billingsameasshipping variable.
<input type="checkbox" data-bind="checked: billingsameasshipping" id="billingsameasshippingcheckbox" />
if checkbox not checked, , billingpostalcode left empty, validation fires correctly. however, if billingpostalcode empty checkbox checked, making field readonly, validation not fired , modelstate's isvalid having value true.
any clues if expected behaviour, or ideas how work around it?
any appreciated. thanks.
edit: added javascript code if helps.
var shipping = { billingsameasshipping: ko.observable(false) }; ko.applybindings(shipping); $("#billingsameasshippingcheckbox").on("change", function () { if (this.checked) { $("#billingpostalcode").val($("#shippingpostalcode").val()); } });
it seems jquery validator uses ignores readonly inputs. managed fix this.
$.validator.setdefaults({ ignore: null });
Comments
Post a Comment