I have this markup on the page:
<div data-bind="visible: found">
<div data-bind="with: eventDetails">
<!-- Some stuff -->
</div>
<div style="clear: left">
<div id="newShow" title="Add a Show"></div>
<a href="#" class="btn btn-primary" id="addShow" data-bind="click: addShow">Add a Show</a> <!-- this is bound to a knockout viewmodel which calls addShowDialog() function -->
</div>
</div>
This is the form which is loaded into div#newShow
<form action="/MyEvents/AddShow/events-385" id="addShowForm" method="post" novalidate="novalidate"> <div>
<div class="editor-label">
<label for="Name">Name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" data-val="true" data-val-required="The Name field is required." id="Name" name="Name" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Date">Date</label>
</div>
<div class="editor-field">
<input class="text-box single-line hasDatepicker" data-val="true" data-val-date="The field Date must be a date." data-val-required="The Date field is required." id="Date" name="Date" type="datetime" value="10 January 2013">
<span class="field-validation-valid" data-valmsg-for="Date" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="ReportingTime">ReportingTime</label>
</div>
<div class="editor-field">
<input class="text-box single-line hasDatepicker" data-val="true" data-val-date="The field ReportingTime must be a date." data-val-required="The ReportingTime field is required." id="ReportingTime" name="ReportingTime" type="datetime" value="03:26 p.m.">
<span class="field-validation-valid" data-valmsg-for="ReportingTime" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="JudgingStarts">JudgingStarts</label>
</div>
<div class="editor-field">
<input class="text-box single-line hasDatepicker" data-val="true" data-val-date="The field JudgingStarts must be a date." data-val-required="The JudgingStarts field is required." id="JudgingStarts" name="JudgingStarts" type="datetime" value="03:26 p.m.">
<span class="field-validation-valid" data-valmsg-for="JudgingStarts" data-valmsg-replace="true"></span>
</div>
</div>
</form>
Here's the Javascript that inserts the form, and handles posting back from it:
function addShowDialog() {
$.get('/myevents/addShow/' + eventId,
null,
function (data, textStatus, xhr) {
refreshTarget($('#newShow'), data);
$('#newShow').dialog('open');
});
};
$('#newShow').dialog({
autoOpen: false,
modal: true,
resizable: false,
buttons: {
"Create a Show": function () {
var form = $('#addShowForm');
var valid = form.valid(); //<-- this always returns true!
if (valid) {
var formData = form.serialize();
formData = formData + '&EventId=' + encodeURIComponent(eventId);
$.post(
form.attr('action'),
formData,
function(data) {
if (data == "Success") {
$('#newShow').dialog("close");
}
}
);
}
},
"Cancel": function () {
$(this).dialog("close");
}
},
close: function () {
loadData();
}
});
I can't get the form in div#newShow
to validate. The check form.valid()
always returns true. My head (and eyeballs) are kinda swimming with the whole thing so I hope this makes sense.
Any ideas please?
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.
Form validation is a process of confirming the relevant information entered by the user in the input field. Here we will be validating a simple form that consists of a username, password and a confirmed password using jQuery. Prerequisites: You must be aware of the basics of HTML, CSS, JavaScript, and jQuery.
According to the jQuery validate documentation for remote: The response is evaluated as JSON and must be true for valid elements, and can be any false, undefined or null for invalid elements, using the default message; or a string, eg. "That name is already taken, try peter123 instead" to display as the error message.
There is an easier way to do this Dany - I prefer to just rebind the form after the ajax response as you can get yourself tied in knots trying to manage what fields have changed or been replaced in the dom.
I have a very large and complex application which makes many changes to many forms so it is easier to just get in the habit of a full rebind. This can be done using:
var forms = $("body").find("form");
for (var i = 0; i < forms.length; i++) {
var form = $(forms[i]);
form.removeData("validator")
.removeData("unobtrusiveValidation");
$.validator.unobtrusive.parse(form);
}
We have a slightly more elegant use of this where a specific container is passed in using the same principal.
The agonising problems to solve happen when an element has been replaced as the reference in the validator is not the same as the new dom element. Very hard to debug and track down. You will find just destroying the validator and reparsing the whole form a 1 size fits all approach and it doesnt matter what's been removed, replaced or otherwise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With