I am working on an ASP MVC3 project, using validate and unobtrusive jquery. I have several area on a page that I need to create custom validation for, and ensure it doesn't break anything else. Currently I have several sections, each have a radio button pair, some text boxes, a button and a table. User fills in the text boxes, hits the button, and I have a function that puts the data from the textboxes into a new row on the table. I also have a counter that is advanced once with each button press.
The validation needs to be along the lines of "If the radio button returned true, the counter needs to be > 1.
I wrote this function that seems like it would work, but I believe I need to re-write it so it is tied into the existing validation, hence the need to add a method or rule:
$(document).ready(function () {
$("#nextBtn").click(function () {
var rows = $("#ROTable tr").length;
if ($("#RO_Yes").is("checked") && (rows < 3))
{
$(this).closest("div.historyCollection").css("backgroundcolor: #ff0000;");
}
});
});
I have looked around and I found a section on the JQUery site discussing Rules (dependant value) which seems like it may fit. But because I am pretty new to this stuff, I wanted to poll the experts before I look too hard in one direction. Thank you for reading.
EDIT: I have been playing around and I came up with this:
SECOND EDIT: As noted by TallMaris, the ROCounter selctor was wrong, so it is now corrected but still fails with the same error:
$(document).ready(function () {
$("#ROCounter").rules("add", {
required: ($("#RO-Yes").is(":checked")) && ($('#ROCounter') < 1),
messages: {
required: "Please enter further information"
}
});
});
but it broke all the validation and caused an error in the jquery.validate.min.js file: " TypeError: Cannot read property 'form' of undefined".
ROCounter is a hidden field. If the error message is attached to this hidden field, does that mean the message will also be hidden, so not show? Here is some HTML:
<div class="formQuestion">
<div class="editor-field2">
@Html.RadioButtonFor(m => m.HistoryModel.EverHadRestrainingOrder, true, new { @class = "commentBox", id="RO_Yes" })
<label for="HistoryModel_ChildAbuseCurrent">Yes</label>
@Html.RadioButtonFor(m => m.HistoryModel.EverHadRestrainingOrder, false, new { @class = "commentBox", id = "RO_No", @checked = "checked" })
<label for="HistoryModel_ChildAbuseCurrent">No</label>
</div>
<div class="editor-label2">
@Html.LabelFor(m => m.HistoryModel.EverHadRestrainingOrder)
</div>
@* Start Fieldset collection for Restraining Order *@
<div class="hidden">
<fieldset class="historyCollection">
<legend>Please provide the following information</legend>
<div class="formGroupHist">
<div class="editor-label-Hist">
@Html.Label("Date of Order")
</div>
<div class="editor-field-Hist">
@Html.TextBox("OrderDate-RO", null, new { @class = "dp" })
</div>
</div>
<div class="formGroupHist" style="width: 125px">
<div class="editor-label-Hist">
@Html.Label("State")
</div>
<div class="editor-field-Hist">
@Html.DropDownList("State-RO", new SelectList(Model.State, "Value", "Text"), "Select", new { style = "width: 65px;" })
</div>
</div>
<div class="formGroupHist">
<div class="editor-label-Hist">
@Html.Label("Name of Protected Party")
</div>
<div class="editor-field-Hist">
@Html.TextBox("ProtectedParties")
</div>
</div>
<div class="formGroupHist">
<div class="editor-label-Hist">
@Html.Label("Explanation of Circumstances")
</div>
<div class="editor-field-Hist">
@Html.TextArea("Comments-RO")
</div>
</div>
<div class="button">
<button type="button" id="ROButton" class="SKButton">Add Information</button>
</div>
<div>
@Html.HiddenFor(m => m.ROCounter)
</div>
<table id="ROTable" class="data-table-page2">
<tr>
<th>Date of Order</th>
<th>State</th>
<th>Name(s) of Protected Parties</th>
<th>Explanation of Circumstances</th>
</tr>
<tr class="ROTempRow">
<td colspan=4>No Information Entered.</td>
</tr>
</table>
</fieldset>
</div>
My main question though, is how to correctly tie the new validation rule into the existing validation. I have read that unobtrusive first parses the document looking for existing validation, and if any is found, it will go no further. The majority of the form is tied into the 'stock' validation so I can not have it broken. Once I get that part figured out, I can probably figure out the actual rule coding for it.
Thank you.
$('#ROCounter'). What type is this ROCounter anyway? can you show a bit of the HTML around?$.validator.addMethod...) and add it to your field. If the "required" rule is what you need but you want it to make it "custom" (indeed, to be calculated only in certain situations and meaning you can accept null values), change the C# property frominttoint?and MVC will not create the required rule for you.