Skip to content

Commit

Permalink
Fields: Fix 'validate=required' for number fields
Browse files Browse the repository at this point in the history
Fixes #309
  • Loading branch information
tabalinas committed Nov 2, 2016
1 parent b247855 commit 984c393
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/fields/jsgrid.field.number.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
},

insertValue: function() {
return parseInt(this.insertControl.val() || 0, 10);
return this.insertControl.val()
? parseInt(this.insertControl.val() || 0, 10)
: undefined;
},

editValue: function() {
return parseInt(this.editControl.val() || 0, 10);
return this.editControl.val()
? parseInt(this.editControl.val() || 0, 10)
: undefined;
},

_createTextBox: function() {
Expand Down
9 changes: 4 additions & 5 deletions src/jsgrid.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,14 +1152,13 @@
};

this._eachField(function(field) {
if(!field.validate)
if(!field.validate ||
($row === this._insertRow && !field.inserting) ||
($row === this._getEditRow() && !field.editing))
return;

var fieldValue = this._getItemFieldValue(item, field);

if(fieldValue === undefined)
return;

var errors = this._validation.validate($.extend({
value: fieldValue,
rules: field.validate
Expand Down Expand Up @@ -1345,7 +1344,7 @@
},

_getEditRow: function() {
return this._editingRow.data(JSGRID_EDIT_ROW_DATA_KEY);
return this._editingRow && this._editingRow.data(JSGRID_EDIT_ROW_DATA_KEY);
},

deleteItem: function(item) {
Expand Down
2 changes: 1 addition & 1 deletion tests/jsgrid.field.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ $(function() {
equal(field.insertTemplate()[0].tagName.toLowerCase(), "input");
equal(field.editTemplate(6)[0].tagName.toLowerCase(), "input");
strictEqual(field.filterValue(), undefined);
strictEqual(field.insertValue(), 0);
strictEqual(field.insertValue(), undefined);
strictEqual(field.editValue(), 6);
});

Expand Down

0 comments on commit 984c393

Please sign in to comment.