crm*_*dev 0 jquery jquery-validate
I've been struggling trying to get custom error messages to display properly in 3 different situations when using the jQuery Validate Plug-in.
I've made note of this post: How to show different error messages based on various conditions. Is it perhaps part of the solution to my issues? I've also looked at Custom error messages for Groups within the jQuery Validation plugin, but it doesn't seem to address the issue of only 1 member of the Group being required and didn't solve my problem.
I'm new to jQuery and fiddles and have a fiddle up which isn't working properly. I suspect its because I don't have links to the graphics used in the CSS which came from the Milk and Marketo examples in the validation plug-in package. The fiddle is at http://jsfiddle.net/cloudmech/myvmb95d/23/.
Here's the code I'm using.
The JQuery:
$(document).ready(function() {
$('#00NU00000049YHZ').change(function(){
var checked = $(this).is(':checked');
$('#company_information').toggle(checked);
$('#company').prop('disabled', !checked)
}).change();
var $CompHdwDon = $('#00NU00000049YHj'),
$SchedPickup = $('#00NU00000049YHt');
$('#w2lForm').validate({
debug: true,
rules:{
company: {
depends: function(element) {
var checked = $('#00NU00000049YHZ').is(':checked');
return (checked);
minlength: 3
}
},
phone: {
require_from_group: [1, '.phone_group'],
phoneUS: true,
},
Mobile: {
require_from_group: [1, '.phone_group'],
phoneUS: true,
},
CompHdwDon: {
required: checked=true
},
SchedPickup: {
required: checked=true
},
},
messages: {
company: {
depends: function(element) {
var checked = $('#00NU00000049YHZ').is(':checked');
return (checked);
required: "Please provide the name of your company"
}
},
phone_group: {
required: "Either your regular or mobile phone is required"
},
phone: {
required: "Either your regular or mobile phone is required"
},
Mobile: {
required: "Either your regular or mobile phone is required"
},
CompHdwDon: {
required: "This box must be checked for us to pick up a donation"
},
SchedPickup: {
required: "This box must be checked to schedule a donation pickup"
},
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
var n = element.attr("name");
if (n == "phone" || n == "Mobile")
error.appendTo(element.parent().next());
else if (element.is(":radio"))
error.appendTo(element.parent().next().next());
else if (element.is(':checkbox'))
error.appendTo(element.parent().next());
else
error.appendTo(element.parent().next());
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
},
highlight: function(element, errorClass) {
$(element).parent().next().find("." + errorClass).removeClass("checked");
}
// specifying a submitHandler prevents the default submit, good for the demo
});
});
Run Code Online (Sandbox Code Playgroud)
The HTML:
<BODY>
<div id="page-content-inner" class="resize">
<form id="w2lForm" action="https://www.xxxxx.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="retURL" value="http://www.google.com">
</br>
<TABLE >
<TR>
<TD class="label">This is for a Company:</TD>
<TD class="field"><input id="00NU00000049YHZ" name="00NU00000049YHZ" type="checkbox" value="0" /></TD>
<TD class="status"></TD>
<!-- If is This is for a Company == false, hide line below. If = true, show line below -->
<TD class="label" colspan="2">
<div id="company_information">
<label id="cmpnylbl" for="company" class="label" >Company:
<input id="company" maxlength="40" name="company" size="20" type="text" class="required" /></div></TD>
<TD class="status"></TD>
</TR>
<!--- create phone group below and require 1 of the two --->
<TR>
<TD class="label"><label for="phone">Phone:</label></TD>
<TD class="field"><input id="phone" class="phone_group" maxlength="40" name="phone" size="20" type="text" /></TD>
<TD class="status"></TD>
<TD class="label"><label for="mobile">Mobile:</label></TD>
<TD class="field"><input id="mobile" class="phone_group" maxlength="40" name="mobile" size="20" type="text" /></TD>
<TD class="status"></TD>
</TR>
<TR>
<TD class="label">Computer Hardware Donation:</TD>
<TD class="field"><input id="00NU00000049YHj" name="00NU00000049YHj" type="checkbox" value="1" class="required" /></TD>
<TD class="status"></TD>
<TD class="label">Schedule Pickup:</TD>
<TD class="field"><input id="00NU00000049YHt" name="00NU00000049YHt" type="checkbox" value="1" class="required" /></TD>
<TD class="status"></TD>
</TR>
<TR><TD class="field" COLSPAN="4"><input type="submit" name="submit"></TD></TR>
</TABLE>
</form>
</div>
</BODY>
Run Code Online (Sandbox Code Playgroud)
The CSS:
body, input, select, textarea { font-family: verdana, arial, helvetica, sans-serif; font-size: 11px; }
img {
border: 0px;
}
#resize{
width: 500px;
}
.clear {
clear: both;
}
input { border: 1px solid black; margin-bottom: .5em; }
input.error { border: 2px solid red; }
label.error {
display: block;
color: red;
font-style: italic;
font-weight: normal;
background: url('images/unchecked.gif') no-repeat;
padding-left: 16px;
margin-left: .3em;
}
label.valid {
background: url('images/checked.gif') no-repeat;
display: block;
width: 16px;
height: 16px;
}
form table td {
padding: 5px;
}
form table input {
width: 200px;
padding: 3px;
margin: 0px;
}
textarea {
width: 400px
}
td.label {
width: 150px;
}
tr.required td.label {
font-weight: bold;
background: url( 'images/backRequiredGray.gif') no-repeat right
center;
}
td.field input.error, td.field select.error, tr.errorRow td.field input,tr.errorRow td.field select {
border: 2px solid red;
background-color: #FFFFD5;
margin: 0px;
color: red;
}
tr td.field div.formError {
display: none;
color: #FF0000;
}
tr.errorRow td.field div.formError {
display: block;
font-weight: normal;
}
#w2lForm .table {
border-spacing: 0px;
border-collapse: collapse;
empty-cells: show;
}
#w2lForm .label {
padding-top: 2px;
padding-right: 8px;
vertical-align: top;
text-align: right;
width: 125px;
white-space: nowrap;
}
#w2lForm .field {
padding-bottom: 10px;
white-space: nowrap;
}
#w2lForm .status {
padding-top: 2px;
padding-left: 8px;
vertical-align: top;
width: 246px;
white-space: nowrap;
}
#w2lForm .textfield {
width: 150px;
}
#w2lForm label.error {
background:url("images/unchecked.gif") no-repeat 0px 0px;
padding-left: 16px;
padding-bottom: 2px;
font-weight: bold;
color: #EA5200;
}
#w2lForm label.checked {
background:url("images/checked.gif") no-repeat 0px 0px;
}
Run Code Online (Sandbox Code Playgroud)
As a final question, when I get this all working, I want to post it using the servlet shown. Do I need to add what's below to my jQuery, do I need to build an Ajax handler or do nothing at all? I've observed that it seems to submit "as is" when the full url to the servlet is available.
submitHandler: function(form) {
form.submit();
}
Run Code Online (Sandbox Code Playgroud)
Revised jQuery:
$(document).ready(function() {
$('#00NU00000049YHZ').change(function(){
var checked = $(this).is(':checked');
$('#company_information').toggle(checked);
$('#company').prop('disabled', !checked)
}).change();
$('#00NU00000049YHZ').on('change', function () { // fire on change
$('[name="company"]').valid(); // evaluate rules on company
});
$('#w2lForm').validate({
debug: true,
rules:{
company: {
required: {
depends: function(element) {
return $('#00NU00000049YHZ').is(':checked');
}
},
minlength: 3
},
phone: {
require_from_group: [1, '.phone_group'],
phoneUS: true,
},
Mobile: {
require_from_group: [1, '.phone_group'],
phoneUS: true,
},
"00NU00000049YHj": {
required: true
},
"00NU00000049YHt": {
required: true
},
},
messages: {
company: {
required: "Please provide the name of your company",
minlength: "Please type at least {0} characters"
},
phone: {
require_from_group: "Either your regular or mobile phone is required"
},
Mobile: {
require_from_group: "Either your regular or mobile phone is required"
},
"00NU00000049YHj": {
required: "This box must be checked for us to pick up a donation"
},
"00NU00000049YHt": {
required: "This box must be checked to schedule a donation pickup"
},
},
// the errorPlacement has to take the table layout into account
errorPlacement: function(error, element) {
var n = element.attr("name");
if (n == "phone" || n == "Mobile")
error.appendTo(element.parent().next());
else if (element.is(":radio"))
error.appendTo(element.parent().next().next());
else if (element.is(':checkbox'))
error.appendTo(element.parent().next());
else
error.appendTo(element.parent().next());
},
// set this class to error-labels to indicate valid fields
success: function(label) {
// set as text for IE
label.html(" ").addClass("checked");
},
highlight: function(element, errorClass) {
$(element).parent().next().find("." + errorClass).removeClass("checked");
}
});
});
Run Code Online (Sandbox Code Playgroud)
Your last question first...
The submitHandler callback function is only needed when you want to change the default behavior of the plugin. The default behavior is that the form submission will be blocked until all data entry passes validation, then it will be submitted to the URL in the action attribute of the form element.
This code is essentially the default, so this entire submitHandler is superfluous and can be left out of .validate()...
submitHandler: function(form) {
form.submit(); // <- already the default behavior
}
Run Code Online (Sandbox Code Playgroud)
However, if you want to submit the form via ajax, then you could use it as follows...
submitHandler: function(form) {
// your ajax function here
return false;
}
Run Code Online (Sandbox Code Playgroud)
Secondly, your code is really broken here...
rules:{
company: {
depends: function(element) {
var checked = $('#00NU00000049YHZ').is(':checked');
return (checked);
minlength: 3
}
},
....
messages: {
company: {
depends: function(element) {
var checked = $('#00NU00000049YHZ').is(':checked');
return (checked);
required: "Please provide the name of your company"
}
},
Run Code Online (Sandbox Code Playgroud)
depends is not a rule; depends is a property of a rule that simply toggles the rule on and off, and you can't have a key:value pair floating inside of a function. This should have been causing all kinds of errors. And you would never see depends inside of messages since the message does not depend on anything... it's simply attached to a rule.
Presumably, you want the company field required depending on another condition...
rules:{
company: {
required: {
depends: function(element) {
return $('#00NU00000049YHZ').is(':checked');
}
},
minlength: 3
},
....
},
messages: {
company: {
required: "Please provide the name of your company",
minlength: "Please type at least {0} characters"
},
....
},
....
Run Code Online (Sandbox Code Playgroud)
Third, what is this??
CompHdwDon: {
required: checked=true
},
SchedPickup: {
required: checked=true
},
Run Code Online (Sandbox Code Playgroud)
You cannot put checked=true as the value when the value of the required method can only be a boolean or a function (depends).
CompHdwDon: {
required: true
},
SchedPickup: {
required: true
},
Run Code Online (Sandbox Code Playgroud)
Finally, the require_from_group method/rule will create a condition where even though you only want one field out of a group to be required, the error message will appear on all of them. The solution is to use the groups option, which will simply group all messages for this particular group of fields into one. Exact placement of the message can then be achieved with the errorPlacement option.
EDIT:
Your code, for example.
rules: {
Mobile: {
require_from_group: [1, '.phone_group'],
phoneUS: true
},
},
messages: {
Mobile: {
required: "Either your regular or mobile phone is required"
},
}
Run Code Online (Sandbox Code Playgroud)
The methods under your rules do not match the methods under messages... you can leave some out but you cannot add any that don't exist.
The corresponding key:value pairs under messages need to match...
messages: {
....
Mobile: {
require_from_group: "Either your regular or mobile phone is required"
},
....
Run Code Online (Sandbox Code Playgroud)
EDIT 2:
CompHdwDon: { // <- this should match the NAME of the field!
required: true
},
Run Code Online (Sandbox Code Playgroud)
This code is doing absolutely nothing because you have no field with name="CompHdwDon". It only appears to work because you have class="required" on the checkbox. In this case, the plugin is using the required class to set the rule. Declare rules within .validate() or declare them inline, but there's no need to do both.
Change this...
<TD class="label">Computer Hardware Donation:</TD>
<TD class="field"><input id="00NU00000049YHj" name="00NU00000049YHj" type="checkbox" value="1" class="required" /></TD>
Run Code Online (Sandbox Code Playgroud)
into this...
<TD class="label">Computer Hardware Donation:</TD>
<TD class="field"><input id="CompHdwDon" name="CompHdwDon" type="checkbox" value="1" /></TD>
Run Code Online (Sandbox Code Playgroud)
EDIT 3:
highlight: function(element, errorClass) {
$(element).parent().next().find("." + errorClass).removeClass("checked");
}
Run Code Online (Sandbox Code Playgroud)
You do not need to append a period to the errorClass object. It's already built in. Also when using highlight, you would typically also need unhighlight.
You also should not need .find(errorClass) as this class is always toggling and highlight is only firing when the field is invalid. Also keep in mind that highlight is only targeting the particular field being evaluated.
highlight: function(element, errorClass) {
$(element).parent().next().removeClass("checked");
},
unhighlight: function(element, errorClass) {
$(element).parent().next().addClass("checked");
}
Run Code Online (Sandbox Code Playgroud)
EDIT 4:
单击复选框时立即触发另一个字段上的错误消息意味着我们需要一个事件处理程序。然后我们简单地触发该.valid()方法来评估该领域的规则。
$('#00NU00000049YHZ').on('change', function () { // fire on checkbox change
$('[name="company"]').valid(); // evaluate rules on company
});
Run Code Online (Sandbox Code Playgroud)
或这个...
// assuming your existing change handler is already working...
$('#00NU00000049YHZ').change(function(){
var checked = $(this).is(':checked');
$('#company_information').toggle(checked);
$('#company').prop('disabled', !checked);
$('[name="company"]').valid(); // evaluate rules on company
}).change();
Run Code Online (Sandbox Code Playgroud)
为了安全起见,请change在您调用.validate().