Skip to content

Commit

Permalink
Reset/Clear form
Browse files Browse the repository at this point in the history
  • Loading branch information
Offir Golan committed Dec 16, 2015
1 parent 5f228da commit 0b22306
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/components/validated-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export default Ember.Component.extend({
run.debounce(this, this.setValue, 500, false);
}),

/**
* When we reset the form, we manually set `value` so we have to make sure to propogate that change
* to the input field
*/
syncRawInputValue: observer('value', function() {
let value = this.get('value');
let rawInputValue = this.get('rawInputValue');

if(value !== rawInputValue) {
this.set('rawInputValue', value);
}
}),

showMessage: computed('attributeValidation.isDirty', 'isInvalid', 'didValidate', function() {
return (this.get('attributeValidation.isDirty') || this.get('didValidate')) && this.get('isInvalid');
}),
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import Ember from 'ember';

const {
get
} = Ember;

export default Ember.Controller.extend({
showAlert: false,
isRegistered: false,
Expand Down Expand Up @@ -38,6 +42,20 @@ export default Ember.Controller.extend({

dismissAlert() {
this.set('showAlert', false);
},

reset() {
let model = this.get('model');
let details = model.get('details');
get(model.constructor, 'attributes').forEach((value, attr) => {
model.set(attr, null);
});
get(details.constructor, 'attributes').forEach((value, attr) => {
details.set(attr, null);
});
model.set('emailConfirmation', null); // Dummy attribute used to confirm the email address so it's not defined in the constructor
this.set('didValidate', false);
this.send('dismissAlert');
}
}
});
1 change: 1 addition & 0 deletions app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
On submit, a manual validation is run which will validate both the <strong>User</strong> and <strong>User Details</strong> models. If both are valid, then the user can continue to the next screen.
</div>
<button id="signup" {{action 'submit'}}>Sign Up</button>
<button id="reset" style="background: #A0A0A0" {{action 'reset'}}>Reset</button>
</div>
</form>
</div>
Expand Down

1 comment on commit 0b22306

@rox163
Copy link

@rox163 rox163 commented on 0b22306 Dec 21, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! just tried it and it works like a charm with my form component!

Please sign in to comment.