Skip to content

Commit

Permalink
make onValidityChange send isValid, isTouched and isInvalidAndTouched
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcobain committed Mar 6, 2017
1 parent cda97bf commit a03eccf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion addon/components/paper-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default Component.extend(ParentMixin, {
return this.get('childComponents').isAny('isInvalid');
}),

isTouched: computed('childComponents.@each.isTouched', function() {
return this.get('childComponents').isAny('isTouched');
}),

submit() {
this.send('onSubmit');
return false;
Expand All @@ -34,7 +38,7 @@ export default Component.extend(ParentMixin, {
actions: {
onValidityChange() {
if (this.get('lastIsValid') !== this.get('isValid')) {
this.sendAction('onValidityChange', this.get('isValid'));
this.sendAction('onValidityChange', this.get('isValid'), this.get('isTouched'), this.get('isInvalid') && this.get('isTouched'));
this.set('lastIsValid', this.get('isValid'));
}
},
Expand Down
17 changes: 14 additions & 3 deletions tests/integration/components/paper-form-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ test('form `onSubmit` action is invoked', function(assert) {
test('form `onValidityChange` action is invoked', function(assert) {
// paper-input triggers `onValidityChange` on render
// so we expect two runs: one on render and another on validity change
assert.expect(2);
assert.expect(6);

this.set('onValidityChange', () => {
assert.ok(true);
this.set('onValidityChange', (isValid, isTouched, isInvalidAndTouched) => {
assert.ok(isValid);
assert.notOk(isTouched);
assert.notOk(isInvalidAndTouched);
});

this.render(hbs`
Expand All @@ -77,13 +79,22 @@ test('form `onValidityChange` action is invoked', function(assert) {
{{/paper-form}}
`);

this.set('onValidityChange', (isValid, isTouched, isInvalidAndTouched) => {
assert.notOk(isValid);
assert.ok(isTouched);
assert.ok(isInvalidAndTouched);
});

this.$('input:first').trigger('blur');

this.set('errors', [{
message: 'foo should be a number.',
attribute: 'foo'
}, {
message: 'foo should be smaller than 12.',
attribute: 'foo'
}]);

});

test('form is reset after submit action is invoked', function(assert) {
Expand Down

0 comments on commit a03eccf

Please sign in to comment.