Skip to content

Commit

Permalink
Override value using data-value attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ajb committed Mar 27, 2013
1 parent 4157ec3 commit bfe44c7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/parsley-standalone.min.js

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

2 changes: 1 addition & 1 deletion dist/parsley.min.js

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

10 changes: 5 additions & 5 deletions parsley.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var Validator = function ( options ) {
/**
* Error messages
*
*
* @property messages
* @type {Object}
*/
Expand Down Expand Up @@ -59,7 +59,7 @@

/**
* Validator list. Built-in validators functions
*
*
* @property validators
* @type {Object}
*/
Expand Down Expand Up @@ -601,7 +601,7 @@
* @returns {String} val
*/
, getVal: function () {
return this.$element.val();
return this.$element.data('value') || this.$element.val();
}

/**
Expand Down Expand Up @@ -1196,7 +1196,7 @@

this.$element.off( '.' + this.type ).removeData( this.type );
}

/**
* reset Parsley binded on the form and its fields
*
Expand Down Expand Up @@ -1272,7 +1272,7 @@

/**
* Parsley plugin configuration
*
*
* @property $.fn.parsley.defaults
* @type {Object}
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
<input type="text" id="remote1" name="remote1" data-remote="http://foo.bar" data-remote-datatype="jsonp" />
<input type="text" id="remote2" name="remote2" data-remote="http://foo.bar/baz" data-trigger="change" data-remote-method="POST" />

<input type="text" id="datavalue1" name="datavalue1" data-value="" value="" data-required="true" />
<input type="text" id="datavalue2" name="datavalue2" data-value="foo" value="" data-required="true" />

<input type="submit" />
</form>
<form id="validate-form" data-validate="parsley">
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,21 @@ var testSuite = function () {
} )
} )

/***************************************
override value with data-value
***************************************/
describe ( 'Override value with data-value' , function () {
it ( 'required - data-value is empty, value is empty', function () {
triggerSubmitValidation( '#datavalue1', '' );
expect( $( '#datavalue1' ).hasClass( 'parsley-error' ) ).to.be( true );
expect( getErrorMessage( '#datavalue1', 'required') ).to.be( 'This value is required.' );
} )
it ( 'required - data-value has value, value is empty', function () {
triggerSubmitValidation( '#datavalue2', '' );
expect( $( '#datavalue2' ).hasClass( 'parsley-success' ) ).to.be( true );
} )
} )

/***************************************
test options changes
***************************************/
Expand Down

0 comments on commit bfe44c7

Please sign in to comment.