joecwallace / jquery-validator Goto Github PK
View Code? Open in Web Editor NEWjQuery implementation of the Laravel validation rules
jQuery implementation of the Laravel validation rules
Maybe I didn't do something properly, but I wasn't able to validate if a checkbox have been checked or not.
Strangely the code of "validate_accept" (validator.js) was checking if the value of the checkbox was "yes" or "1" (line #190):
return this.validate_match( attribute, value, /^(yes|1)$/ );
But if you create a checkbox this way:
{{ Form::checkbox('cb') }}
by default the value will be "1", so the code of "validate_accept" will always return true.
What I did to solve the issue:
validate_accepted : function( attribute, value ) {
return $("input[name='"+attribute+"']").attr('checked');
},
See my commit:
https://github.com/FR6/jquery-validator/commit/7a9fde69c2018e3afe5d64ee541185ebcf42ebef
From laravel validation rules: The size validation rule should validate that an attribute is a given length, or, if an attribute is numeric, is a given value.
It would be great if the jquery validator could be enhanced to include warnings too. Thus validating an input element to an error showing it with a red background and preventing to submit the form, and validating an input element to a warning showing it with a yellow background but allowing to submit the form.
At the moment I just copied the validator.js file and renamed it into warnings.js and replaced data-validations by data-warnings. Then I can use data-validations rules for errors and data-warnings rules for warnings. I use
Validation crashes when it is possible to make multiple selections using a 'select' element. I have to add the following code in the validate_required() function to make it work.
if ($("[name="" + attribute + ""]").attr('multiple') === 'multiple') {
return $("[name="" + attribute + ""] option:selected").size() > 0;
}
Scenario: The first submit of the form fails due to some validation rule. The user then clicks on a radio button that displays another input element previously hidden and a new validation rule is added for that element using some java script. When resubmitting the form the new validation rule is not read into the validation rules.
This is due to jQuery. The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery). See http://api.jquery.com/data (and search for 'mutated').
Solution: Use
Thus line 45 should read: var validation_rules = $(this).attr('data-' + attribute),
I'm using the Chosen JavaScript plugin from Harvest (http://harvesthq.github.io/chosen). It uses an input element without the 'name' attribute.
I added the following code in the validate() function to make it work.
if (name === undefined) {
return true;
}
I get a syntax error, unrecognized expression name=mychkbox[] when I submit the form. It is general practice to add square brackets [] to the name of checkboxes to inform PHP that the value is an array of multiple values. However, in validator.js the following code will make JavaScript crash:
if ($("[name=" + attribute + "]").first().is(':radio')) {}
...
return value === $("[name=" + parameters[0] + "]").val();
Instead use:
if ($('[name="' + attribute + '"]').first().is(':radio')) {}
...
return value === $('[name="' + parameters[0] + '"]').val();
All attribute and parameters[0] should be enclosed in double quotes to prevent the error from occurring.
validate_accepted() always returns true because $().val() will return the value attribute regardless of checked status. Perhaps the ":checked" test should be moved before the validate_match test.
validate_accepted : function (attribute, value) {
return this.validate_match(attribute, value, /^(yes|1)$/) ||
$("[name=\"" + attribute + "\"]").is(":checked");
},
My html:
<input type="checkbox" value="1" name="myName" data-validations="accepted">
There was errors on IE7 and IE8 stoping completely the Javascript.
Using JSHint I corrected some errors and warnings:
Hi, I like the plugin, simple. But i wish you can add something like, a done: {} callback,
....
callback: function(el, valid)
{
....
},
done: function(valid) // called once
{
....
}
....
Thanks is advanced!
Is there a way to know which validation failed? E.g. when I have the rules "required" and "email", and I want to know which one didn't go through?
I'd like to use this for printing an error message. Is this already possible?
Thanks in advance.
Laravel 4 doesn't support bundles, how to install with composer?
Hey,
in the description on how to valiate on each input, there's missing an "!":
if(valid) {
$( elem ).addClass( 'error' );
}
should be
if(! valid) {
...
}
Just a quick tip for other people struggling with this. This function is returning 'elem' as a string, not an object. Here's how you parse it:
callback : function( elem, valid ) {
var elem_id = $(elem).attr("id");
if ( valid ) {
$('span#' +elem_id).html('<img src="/resources/images/icons/tick_circle.png" />');
}
else {
$('span#' +elem_id).html('<img src="/resources/images/icons/cross_circle.png" />');
}
}
LF's get converted to CRLF's when the form is submitted. Thus the length on the client side is less than the length on the server side (if textarea contains one or more line feeds). See also http://stackoverflow.com/questions/462348/string-length-differs-from-javascript-to-java-code.
Suppose a database field for storing the textarea contents is set to a text field of max 255 characters. We want to make sure that the textarea contents will fit into that database field. Meaning we should count any line feeds in the textarea twice on the client side.
Solution: Use return value.replace(/\n/g, '\r\n').length; instead of return value.length; in the size() function.
Hey.
Thanks for such an awesome validator. I have been using this for quite a while now & quite happy about this.
Currently, I am working on a project which needs client side file size validations. I went ahead & used the size validation rule.
It doesn't seem to be working the way it is supposed to.
Is this a known issue? What do you recommend to use?
Thanks.
can you update the installation for composer?
Due to the $.each statement on line 24
$.each(rules.split("|"), function (idx, value) {
}
failing when the rules parameter is undefined.
Fix: Wrap the following if statement around the $.each statement.
if (rules) {
}
please provide us a composer json code for this project so that we can use it .
thanks . :)
When using the "min:10|max:50" rule, the numbers 2, 3, 4, 5 are accepted whilst they should not. This is due to the validator using string comparisons instead of numeric comparisons. Use return parseFloat(this.size(attribute, value)) >= parseFloat(parameters[0]); instead of return this.size(attribute, value) >= parameters[0];
See also http://www.jsworkshop.com/bb/viewtopic.php?t=14
Something like this for hexadecimal numbers:
$(document).ready(function() { $('#myForm').validator({ events : 'submit', selector : 'input[type!=submit], select, textarea', // Custom validation. validations: validate_hexadecimal = function (attribute, value) { return this.validate_match(attribute, value, /^[0-9A-F]+$/i); }; callback : function( elem, valid ) { if ( ! valid ) { $( elem ).addClass('error'); } } }); });
Validation fails when all radio buttons are unchecked and the data-validations attribute is set to "required".
It would be great to automatically generate the "data-validations" when passing a validator to the Form. This way we could specify the rules in only one place.
If we would want to implement this change, I think we would have to overwrite the Laravel\Form class so we could pass im a validator end then when generating the fields we would look at the validator to see if there is a data-validations to add.
This awesome package was obviously prepared for Laravel 3, is there any hope someone could prepare a Laravel 4 version of this?
Hey,
could you create a package for composer for using this library in Laravel 4?
I'd do it myself, but i'm not yet too familiar with Laravel, Composer and Packalyst.
EDIT: I don't even know how to tag this issue as a question. :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.