Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/guillaumepotier/Parsley.js …
Browse files Browse the repository at this point in the history
…into luhn-validator

Conflicts:
	dist/parsley.extend.min.js
	parsley.extend.js
	tests/index.html
	tests/tests.js
  • Loading branch information
Xavier Zwirtz committed Mar 6, 2013
2 parents eb36bd3 + 0bf21b7 commit e223bca
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 117 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#Changelog

**1.1.14-dev (next version)**

- added luhn validator in parsley.extra (#150)
- added inlist validator in parsley.extra (#153)
- added _messages.en.js template in localization folder
- fixed "Uncaught RangeError: Maximum call stack size exceeded" on jQuery `.off()`
Refs #136
- added $('#form').parsley('isValid') to know if form constraints fails, without
adding DOM errors. Refs #94

**1.1.13 (current stable)**

- added jquery plugin manifest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Add new validators in `parsley.extend.js` and minify it. No validators will be a
##Localization

If file does not exist, create it into `ì18n/` directory with same syntax as others.
Reference file is French one! ;)
Reference file is _messages.en.fr

##Integrations

Expand Down
149 changes: 75 additions & 74 deletions dist/parsley-standalone.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions dist/parsley.extend.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 25 additions & 24 deletions dist/parsley.min.js

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,14 @@ <h4>Form</h4>
<pre><code>$( '#form' ).parsley( 'validate' );</code></pre>
</td>
</tr>
<tr>
<td>Test if form valid</td>
<td>Boolean</td>
<td>Useful if you want to integrate the form validation process inside custom functions, without triggering error messages.</td>
<td class="not-for-mobile">
<pre><code>$( '#form' ).parsley( 'isValid' );</code></pre>
</td>
</tr>
<tr>
<td>Destroy Parsley</td>
<td></td>
Expand Down Expand Up @@ -1326,6 +1334,13 @@ <h5>Here is the list of parsley.extra validators</h5>
<td><code>data-after-date="#elem"</code></td>
<td>Validate that a field's date is after #elem's date.</td>
</tr>
<tr>
<td>In list</td>
<td><code>data-inlist="foo, bar, foo bar"</code></td>
<td>
Validates that a field's value is present within the value list. You can define the delimiter using <code>data-inlist-delimiter=","</code>. Delimiter defaults to <code>","</code>.
</td>
</tr>
</tbody>
</table>
</section>
Expand Down
47 changes: 47 additions & 0 deletions i18n/_messages.en.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* /!\ This file is just an example template to create/update your own language file /!\
*/

window.ParsleyConfig = window.ParsleyConfig || {};

(function ($) {
window.ParsleyConfig = $.extend( true, {}, window.ParsleyConfig, {
messages: {
// parsley //////////////////////////////////////
defaultMessage: "This value seems to be invalid."
, type: {
email: "This value should be a valid email."
, url: "This value should be a valid url."
, urlstrict: "This value should be a valid url."
, number: "This value should be a valid number."
, digits: "This value should be digits."
, dateIso: "This value should be a valid date (YYYY-MM-DD)."
, alphanum: "This value should be alphanumeric."
}
, notnull: "This value should not be null."
, notblank: "This value should not be blank."
, required: "This value is required."
, regexp: "This value seems to be invalid."
, min: "This value should be greater than %s."
, max: "This value should be lower than %s."
, range: "This value should be between %s and %s."
, minlength: "This value is too short. It should have %s characters or more."
, maxlength: "This value is too long. It should have %s characters or less."
, rangelength: "This value length is invalid. It should be between %s and %s characters long."
, mincheck: "You must select at least %s choices."
, maxcheck: "You must select %s choices or less."
, rangecheck: "You must select between %s and %s choices."
, equalto: "This value should be the same."

// parsley.extend ///////////////////////////////
, minwords: "Aquest valor ha de tenir %s paraules com a mínim."
minwords: "This value should have %s words at least."
, maxwords: "This value should have %s words maximum."
, rangewords: "This value should have between %s and %s words."
, greaterthan: "This value should be greater than %s."
, lessthan: "This value should be less than %s."
, beforedate: "This date should be before %s."
, afterdate: "This date should be after %s."
}
});
}(window.jQuery || window.Zepto));
7 changes: 7 additions & 0 deletions parsley.extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ window.ParsleyConfig = window.ParsleyConfig || {};
return Date.parse($(elem).val()) < Date.parse(val);
}

, inlist: function ( val, list, self ) {
var delimiter = self.options.inlistDelimiter || ',';
var listItems = (list + "").split(new RegExp("\\s*\\" + delimiter + "\\s*"));

return (listItems.indexOf(val.trim()) !== -1);
}

, luhn: function ( val, elem, self) {
val = val.replace(/[ -]/g, '');
var digit, n, sum, _j, _len1, _ref2;
Expand Down
44 changes: 31 additions & 13 deletions parsley.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@
var ParsleyField = function ( element, options, type ) {
this.options = options;
this.Validator = new Validator( options );

// if type is ParsleyFieldMultiple, just return this. used for clone
if ( type === 'ParsleyFieldMultiple' ) {
return this;
}

this.init( element, type || 'ParsleyField' );
};

Expand Down Expand Up @@ -554,24 +560,25 @@
this.$element.addClass( 'parsley-validated' );

// remove eventually already binded events
if ( this.$element.data( 'events' ) ) {
this.$element.off( '.' + this.type );
}
this.$element.off( '.' + this.type );

// force add 'change' event if async remote validator here to have result before form submitting
if ( this.options.remote && !new RegExp( 'change', 'i' ).test( this.options.trigger ) ) {
this.options.trigger = !this.options.trigger ? 'change' : ' change';
}

// alaways bind keyup event, for better UX when a field is invalid
var triggers = ( !this.options.trigger ? '' : this.options.trigger + ' ' )
+ ( new RegExp( 'key', 'i' ).test( this.options.trigger ) ? '' : 'keyup' );
var triggers = ( !this.options.trigger ? '' : this.options.trigger )
+ ( new RegExp( 'key', 'i' ).test( this.options.trigger ) ? '' : ' keyup' );

// alaways bind change event, for better UX when a select is invalid
if ( this.$element.is( 'select' ) ) {
triggers += new RegExp( 'change', 'i' ).test( triggers ) ? '' : ' change';
}

// trim triggers to bind them correctly with .on()
triggers = triggers.replace( /^\s+/g , '' ).replace( /\s+$/g , '' );

this.$element.on( ( triggers + ' ' ).split( ' ' ).join( '.' + this.type + ' ' ), false, $.proxy( this.eventValidation, this ) );
}

Expand Down Expand Up @@ -824,6 +831,7 @@
this.removeErrors();
this.validatedOnce = false;
this.errorClassHandler.removeClass( this.options.successClass ).removeClass( this.options.errorClass );

return this;
}

Expand Down Expand Up @@ -890,7 +898,6 @@
*/
, destroy: function () {
this.$element.removeClass( 'parsley-validated' );
this.errorClassHandler.removeClass( this.options.errorClass ).removeClass( this.options.successClass );
this.reset().$element.off( '.' + this.type ).removeData( this.type );
}
};
Expand Down Expand Up @@ -943,7 +950,7 @@
* @param {Object} options
*/
, inherit: function ( element, options ) {
var clone = new ParsleyField( element, options );
var clone = new ParsleyField( element, options, 'ParsleyFieldMultiple' );

for ( var property in clone ) {
if ( 'undefined' === typeof this[ property ] ) {
Expand Down Expand Up @@ -1005,18 +1012,19 @@
this.$element.addClass( 'parsley-validated' );

// remove eventually already binded events
if ( this.$element.data( 'events' ) ) {
this.$element.off( '.' + this.type );
}
this.$element.off( '.' + this.type );

// alaways bind keyup event, for better UX when a field is invalid
var self = this
, triggers = ( !this.options.trigger ? '' : this.options.trigger + ' ' )
+ ( new RegExp( 'change', 'i' ).test( this.options.trigger ) ? '' : 'change' );
, triggers = ( !this.options.trigger ? '' : this.options.trigger )
+ ( new RegExp( 'change', 'i' ).test( this.options.trigger ) ? '' : ' change' );

// trim triggers to bind them correctly with .on()
triggers = triggers.replace( /^\s+/g , '' ).replace( /\s+$/g ,'' );

// bind trigger event on every siblings
$( this.siblings ).each(function () {
$( this ).on( triggers.split( ' ' ).join( '.' + self.type + ' ' ), false, $.proxy( self.eventValidation, self ) );
$( this ).on( triggers.split( ' ' ).join( '.' + self.type + ' ' ) , false, $.proxy( self.eventValidation, self ) );
} )
}
};
Expand Down Expand Up @@ -1139,6 +1147,16 @@
return isValid;
}

, isValid: function () {
for ( var item = 0; item < this.items.length; item++ ) {
if ( false === this.items[ item ].isFieldValid() ) {
return false;
}
}

return true;
}

/**
* Remove all errors ul under invalid fields
*
Expand Down
11 changes: 10 additions & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,17 @@
<input type="text" id="reset-email" data-required="true" />
<textarea id="reset-textarea" data-required="true"></textarea>
</form>
<form data-validate="parsley" id="dynamic-form">
<form data-validate="parsley" id="dynamic-form" data-triggers="keyup change">
</form>
<form data-validate="parsley" id="onthefly-form">
<input type="text" id="onthefly" />
</form>
<form dat-validate="parsley" id="dataerrorcontainer-form">
<input id="dataerrorcontainer" type="text" required data-error-container="#mycustomerrorcontainer" />
</form>
<form dat-validate="parsley" id="isValid-form">
<input id="isValid-field" type="text" required />
</form>
<div id="mycustomerrorcontainer"></div>
<form data-validate="parsley" id="onFieldValidate-form">
<input type="text" id="onFieldValidate1" data-type="email" data-required="true" />
Expand Down Expand Up @@ -227,6 +230,12 @@
<input type="text" id="beforeDate" data-beforedate="#beforeDate-model" />
<input type="text" id="afterDate-model" value="1/1/2014" />
<input type="text" id="afterDate" data-afterdate="#afterDate-model" />
<input type="text" id="inList" data-inlist="true, 1, valid, value with spaces, yes, one" />
<input type="text" id="inListSingleValue" data-inlist="true" />
<input type="text" id="inListEmpty" data-inlist="" />
<input type="text" id="inListSingleComma" data-inlist="," />
<input type="text" id="inListCustomDelimiter" data-inlist="foo | bar | foo bar" data-inlist-delimiter="|" />

<input type="text" id="luhn" data-luhn="true" />
</form>
</div>
Expand Down
37 changes: 36 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,18 @@ var testSuite = function () {
expect( $( '#dataerrorcontainer-form' ).parsley( 'validate' ) ).to.be( false );
expect( $( '#mycustomerrorcontainer ul.parsley-error-list' ).length ).to.be( 1 );
} )
it ( 'test isValid', function () {
expect( $( '#isValid-form' ).parsley( 'isValid' ) ).to.be( false );
expect( $( '#isValid-field' ).hasClass( 'parsley-success' ) ).to.be( false );
expect( $( '#isValid-field' ).hasClass( 'parsley-error' ) ).to.be( false );
$( '#isValid-field' ).val( 'foo' );
expect( $( '#isValid-form' ).parsley( 'isValid' ) ).to.be( true );
expect( $( '#isValid-field' ).hasClass( 'parsley-success' ) ).to.be( false );
expect( $( '#isValid-field' ).hasClass( 'parsley-error' ) ).to.be( false );
$( '#isValid-field' ).val( '' );
expect( $( '#isValid-form' ).parsley( 'validate' ) ).to.be( false );
expect( $( '#isValid-field' ).hasClass( 'parsley-error' ) ).to.be( true );
} )
} )

/***************************************
Expand Down Expand Up @@ -957,6 +969,30 @@ var testSuite = function () {
triggerSubmitValidation( '#rangewords', 'foo bar baz foo bar baz foo' );
expect( $( '#rangewords' ).hasClass( 'parsley-success' ) ).to.be( true );
} )
it ( 'inlist validation', function () {
triggerSubmitValidation( '#inList', 'invalid' );
expect( $( '#inList' ).hasClass( 'parsley-error' ) ).to.be( true );
triggerSubmitValidation( '#inList', 'false' );
expect( $( '#inList' ).hasClass( 'parsley-error' ) ).to.be( true );
triggerSubmitValidation( '#inList', 'true' );
expect( $( '#inList' ).hasClass( 'parsley-success' ) ).to.be( true );
triggerSubmitValidation( '#inList', 'one' );
expect( $( '#inList' ).hasClass( 'parsley-success' ) ).to.be( true );
triggerSubmitValidation( '#inList', 'value with spaces' );
expect( $( '#inList' ).hasClass( 'parsley-success' ) ).to.be( true );

triggerSubmitValidation( '#inListSingleValue', 'true' );
expect( $( '#inListSingleValue' ).hasClass( 'parsley-success' ) ).to.be( true );

triggerSubmitValidation( '#inListEmpty', 'foo' );
expect( $( '#inListEmpty' ).hasClass( 'parsley-error' ) ).to.be( true );

triggerSubmitValidation( '#inListSingleComma', 'value' );
expect( $( '#inListSingleComma' ).hasClass( 'parsley-error' ) ).to.be( true );

triggerSubmitValidation( '#inListCustomDelimiter', 'foo bar' );
expect( $( '#inListCustomDelimiter' ).hasClass( 'parsley-success' ) ).to.be( true );
} )
it ( 'greaterThan', function () {
triggerSubmitValidation( '#greaterThan', '1' );
expect( $( '#greaterThan' ).hasClass( 'parsley-error' ) ).to.be( true );
Expand Down Expand Up @@ -996,7 +1032,6 @@ var testSuite = function () {
triggerSubmitValidation( '#luhn', '4000000000000002' );
expect( $( '#luhn' ).hasClass( 'parsley-success' ) ).to.be( true );
} )

} )

} )
Expand Down

0 comments on commit e223bca

Please sign in to comment.