Kendo UI - Validator message overwrite and empty errors array - kendo-ui

I'm running into a problem with a Kendo validator. Three fields are set up for validation. Tabbing through them, the first name and last name fields show the expected message, but after tabbing through the from date field, the first name and last name messages get replaced with the from date message. Then, upon calling the validate() method, the errors array is empty even though validation fails. Also, if the save button is clicked without tabbing through the fields, the errors for the fields are not displayed at all.
Another strange thing is that if the spans for the messages are removed, all messages appear next to the first name field - they aren't displayed next to the corresponding input.
Any insight as to get this working properly would be appreciated.
the Script and HTML:
$(document).ready(function() {
$("#fn").kendoMaskedTextBox();
$("#ln").kendoMaskedTextBox();
$("#from").kendoDatePicker({
format: "MM/dd/yyyy"
});
$("#thru").kendoDatePicker({
format: "MM/dd/yyyy"
});
$("#btnSave").kendoButton({
icon: "tick",
click: function() {
if (!vld.validate())
alert(vld.errors.length);
}
});
var vld = $("#myForm").kendoValidator().data('kendoValidator');
})
<div id="myForm">
<p>
<label for="fn" class="myLabel">First Name:</label>
<input id="fn" required validationMessage="{0} Required" />
<span class="k-invalid-msg" data-for="fn"></span>
</p>
<p>
<label for="ln" class="myLabel">Last Name:</label>
<input id="ln" required validationMessage="{0} Required" />
<span class="k-invalid-msg" data-for="ln"></span>
</p>
<p>
<label for="from" class="myLabel">Period:</label>
<input id="from" required validationMessage="From date is required" />
<span> - </span>
<input id="thru" />
<span class="k-invalid-msg" data-for="from"></span>
</p>
<p>
<label class="myLabel"></label>
<button id="btnSave" class="btn">Save</button>
</p>
</div>
jsFiddle link: http://jsfiddle.net/artrfkmw/1/

Okay, it seems a/the trick to make this work is to set the values of the data-for attributes of the message spans to the name attribute of the input tag, not the id tag. So in the above example, adding name tags to the input elements is the quick fix:
<div id="myForm">
<p>
<label for="fn" class="myLabel">First Name:</label>
<input id="fn" name="fn" required validationMessage="{0} Required" />
<span class="k-invalid-msg" data-for="fn"></span>
</p>
<p>
<label for="ln" class="myLabel">Last Name:</label>
<input id="ln" name="ln" required validationMessage="{0} Required" />
<span class="k-invalid-msg" data-for="ln"></span>
</p>
<p>
<label for="from" name="from" class="myLabel">Period:</label>
<input id="from" required validationMessage="From date is required" />
<span> - </span>
<input id="thru" />
<span class="k-invalid-msg" data-for="from"></span>
</p>
<p>
<label class="myLabel"></label>
<button id="btnSave" class="btn" >Save</button>
</p>
</div>

Related

Laravel Blade template checkbox checked when another one is checked

I have the following checkboxes in view
<div class="form-group">
<label class="col-md-4 control-label">Roles</label>
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="data_entry"> Data Entry <br />
<input type="checkbox" name="save_on_ext_hd"> Save On External HD <br />
<input type="checkbox" name="print"> Print <br />
<input type="checkbox" name="export_csv">Export CSV <br />
<input type="checkbox" name="delete"> Delete
</label>
</div>
</div>
</div>
I am using laravel 5 and blade template.
the issue is: when click on any checkbox the first one checked and verse versa
Ex: when click pn print checkbox the data_entry checked automatically and when unchecked print data_entry unchecked.
also when check print data_entry checked and when check delete for example data_entry unchecked.
That's because all your checkboxes are inside one <label>. Labels with an input inside activate the input when clicked. So clicking anywhere inside the label will trigger all checkboxes.
Solve this by giving each its own <label>:
<label>
<input type="checkbox" name="data_entry"> Data Entry <br />
</label>
<label>
<input type="checkbox" name="save_on_ext_hd"> Save On External HD<br />
</label>
<!-- etc -->

MVC 3 Format Validation Messages

I am working with a form layout that has 4 input fields per row so I need to find a different way to display the validation messages. I was thinking I could display any messages above the form but would like to add a border and a background color.
I am using unobtrusive JS for validation.
Is there a way to detect if there are validation errors from the view?
Example
#using (Ajax.BeginForm("ProcessContactInfo", ajaxOpts))
{
#Html.ValidationSummary(true)
if (!ViewData.ModelState.IsValid)//does nothing because using unobtrusiveJS validation
{
<div id="ValidationResultsArea" style="padding:20px; border: 1px solid #000000; background-color: #ffffcc; display: none" >
#Html.ValidationMessageFor(model => model.Contact.FirstName)<br />
#Html.ValidationMessageFor(model => model.Contact.LastName)<br />
#Html.ValidationMessageFor(model => model.Contact.Email)<br />
#Html.ValidationMessageFor(model => model.Contact.Message)<br />
</div>
}
<p><span class="required">* All fields are required.</span></p>
}
Looks like you are looking for something similar to this....
If that is what you are looking for, then you can easily achieve it if you are using MVC3 and jquery validation. jQuery validation uses custom data attributes, css and unobtrusive javascript to perform user input validation, so when validation fails for a specific input field, jQuery "injects" data attributes and css classes, then you can take advantage of this by using css to define how you want to the fields to look like. Here's the HTML belonging to the screenshot above...
<div class="editor-label">
<label for="UserName" style="display: none;">User name</label>
</div>
<div class="editor-field">
<span class="field-validation-error" data-valmsg-for="UserName" data-valmsg-replace="true">The User name field is required.</span>
<input class="input-validation-error" data-val="true" data-val-required="The User name field is required." id="UserName" name="UserName" placeholder="Username" type="text" value="">
</div>
<div class="editor-label">
<label for="Password" style="display: none;">Password</label>
</div>
<div class="editor-field">
<span class="field-validation-error" data-valmsg-for="Password" data-valmsg-replace="true">The Password field is required.</span>
<input class="input-validation-error" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" placeholder="Password" type="password">
</div>
<div class="editor-label">
<input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true"><input name="RememberMe" type="hidden" value="false"> Remember Me
</div>
<p>
<input type="submit" value="Log On">
</p>
You can see that jQuery Validation framework added a class (input-validation-error) to the Username input field. You can specify override this css class in your project.

Form fields in acordina are not geting validated

I am using validationEngine for validating form fields. I have tab and inside the tab, I have two accordions. Each accordion has 4 fields. When I submit, only first accordion form fields that are opened are getting validated. Is there any way I can force to validate all the form fields and open all accordions to show the required field message? Thank you!
<div id="tabs-1">
<div id="accordion">
<h3>Select Agent</h3> <!-- First accordion title) -->
<fieldset>
<div>
<label >Agent Name or ID</label>
<input name="insuredFirsName" title="type "Agent name or id "" class="validate[required] text-input" type="text" >
</div>
<label>Age</label>
<input type="text" name="age" value="${ajaxForm.age}" maxlength="3" size="3" class="validate[required] text-input" >
</fieldset>
<h3>Application Details</h3> <!-- Second accordion title) -->
<fieldset>
<div>
<label>Contract State</label>
<select name="State" id="State" class="validate[required]">
<option value="">Choose a State</option>
<option value="IL">Illinois</option>
<option value="MN">Minnesota</option>
<option value="WI">Wisconsin</option>
</select>
</div>
<div>
<label>Application Number :</label>
<input value="" class="validate[required] text-input" type="text" name="req" id="req" />
</div>
<div>
<label>Application Number :</label>
<input value="" class="validate[required] text-input" type="text" name="req1" id="req1" />
</div>
</fieldset>
</div> <!-- Accordion close -->
<html:button property="button1" value ="Submit"></html:button>
</div> <!-- Tab-1 close -->
maybe a little late but I had this same problem, i searched on the internet and found a solution, but this will work for you:
jQuery("#digitale-aanvraag").validationEngine({
validateNonVisibleFields: true,
});
Your fields in an accordion are set to hidden so as default the validator would skip them.
Be aware that if you use this in a wizard, it won't work, because those fields are also hidden...
Anyway, GL!

Grails, submit form in modal with ajax

Right now I can render a signup form in a twitter bootstrap modal, enter data into it, if the data doesn't pass constraints, the form will render correctly and say that the constraints don't pass (via error messages).
But the problem is, the formRemote that I'm using ends up "passing" even though the constraints aren't validated and the view I get contains the error message.
Essentially, I create a modal, then have it's data-remote set to the gsp containing this code:
<%# page import="security.User" %>
<html>
<head>
<r:layoutResources />
<g:set var="entityName" value="${message(code: 'user.label', default: 'User')}" />
<title><g:message code="default.create.label" args="[entityName]" /></title>
</head>
<body>
<g:hasErrors bean="${userInstance}">
<ul class="errors" role="alert">
<g:eachError bean="${userInstance}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
<g:formRemote name="ajaxSignupForm" url="[action:'save',controller:'user']" update="signupModal .modal-body">
<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'username', 'error')} required">
<label for="username">
<g:message code="user.username.label" default="Username" />
<span class="required-indicator">*</span>
</label>
<g:textField name="username" required="" value="${userInstance?.username}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'password', 'error')} required">
<label for="password">
<g:message code="user.password.label" default="Password" />
<span class="required-indicator">*</span>
</label>
<g:passwordField name="password" required="" value="${userInstance?.password}"/>
</div>
<div class="fieldcontain ${hasErrors(bean: userInstance, field: 'confirmPassword', 'error')} required">
<label for="confirmPassword">
<g:message code="user.confirmPassword.label" default="Confirm Password" />
<span class="required-indicator">*</span>
</label>
<g:passwordField name="confirmPassword" required="" value="${userInstance?.confirmPassword}"/>
</div>
<g:submitButton name="create" class="btn btn-success" value="Create Account" />
</g:formRemote>
<div style='display: none; text-align: left;' id='signupMessage'></div>
<r:layoutResources />
</body>
</html>
The update="signupModal .modal-body" chunk is what is updating the modal that this gsp is contained in. I understand that this is some nasty logic, so what can I do to make this better and actually perform correctly?
I prefer to enclose your formRemote inside a div. and then set the update="formParent".
E.g.
<div id="formParent" ... update="signupModal">
<g:formRemote
...
</div>

Jquery Validation plug-in custom error placement

Using the jQuery Validation plug-in for the following form:
<form id="information" method="post" action="#">
<fieldset>
<legend>Please enter your contact details</legend>
<span id="invalid-name"></span>
<div id="id">
<label for="name">Name: (*)</label>
<input type="text" id="name" class="details" name="name" maxlength="50" />
</div>
<span id="invalid-email"></span>
<div id="id">
<label for="email">Email: (*)</label>
<input type="text" id="email" class="details" name="email" maxlength="50" />
</div>
</fieldset>
<fieldset>
<legend>Write your question here (*)</legend>
<span id="invalid-text"></span>
<textarea id="text" name="text" rows="8" cols="8"></textarea>
<div id="submission">
<input type="submit" id="submit" value="Send" name="send"/>
</div>
<p class="required">(*) Required</p>
</fieldset>
</form>
How can I place the errors inside the span tags? (#invalid-name, #invalid-email, #invalid-text)
I read the documentation about error placement but I did not get how it works.
Is it possible to handle each single error and place it in the specified element?
Thank you
You can also manually add error labels in places you need them. In my particular case I had a more complex form with checkbox lists etc. where an insert or insert after would break the layout. Rather than doing this you can take advantage of the fact that the validation script will evaluate if an existing label tag exists for the specified field and use it.
Consider:
<div id="id">
<label for="name">Name: (*)</label>
<input type="text" id="name" class="details" name="name" maxlength="50" />
</div>
Now add the following line:
<label for="name" class="error" generated="true"></label>
which is standard error label:
<div id="id">
<label for="name">Name: (*)</label>
<input type="text" id="name" class="details" name="name" maxlength="50" />
</div>
<div id="id-error">
<label for="name" class="error" generated="true"></label>
<div>
jQuery will use this label rather than generating a new one. Sorry I could not find any official documentation on this but found other posts that came across this behaviour.
This is a basic structure, you can use whatever selector you would like in the method. You have the error element and the element that was invalid.
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.appendTo(element.prev());
}
});
Or to target the ID, you could do
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.appendTo('#invalid-' + element.attr('id'));
}
});
Not tested, but should work.
I found that using .insertAfter rather than .appendTo works:
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.insertAfter('#invalid-' + element.attr('id'));
}
});
I'm using the metadata extension with the validator.. (note, I'm setting it to use the data-meta attribute on the markup...)
<input ... data=meta='{
errorLabel: "#someotherid"
,validate: {
name:true
}
}' >
then in code...
jQuery.validator.setDefaults({
errorPlacement: function(error, element) {
error.appendTo($(
$(element).metadata().errorLabel
));
}
});
I've been using the metadata for a lot of similar functionality, which works rather nicely... note, I used the single ticks (apostrophes) around the meta data, this way you can use a JSON serializer server-side to inject into that portion of the tag (which should use double-quotes around strings)... a literal apos may be an issue though, (replace "'" with "\x27" in the string).

Resources