Skip to content

Commit 697b1dc

Browse files
committed
Add multiple actions support
1 parent cf7d373 commit 697b1dc

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ abstraction that allows to know when the form is submitting or not (so you can u
109109
To do that, you must wrap your inputs around the `validatable-form` component:
110110
111111
```html
112-
{{#validatable-form onSubmit=(action 'saveUser') as |component|}}
112+
{{#validatable-form onSubmit=(action 'saveUser') as |form|}}
113113
{{#each-in component.errors as |field error|}}
114114
{{field}}: {{error}}
115115
{{/each-in}}
@@ -128,7 +128,7 @@ To do that, you must wrap your inputs around the `validatable-form` component:
128128
<p>Currently saving...</p>
129129
{{/if}}
130130
131-
<input type="submit" value="Submit form">
131+
<input {{action 'submit' target=form}} value="Submit form">
132132
{{/validatable-form}}
133133
```
134134
@@ -145,9 +145,25 @@ the ID for something more meaningful, you can add the `data-field` attribute to
145145
{{input type='password' class='form__input' data-field='Password' required=true}}
146146
```
147147
148+
The submit button must contain an action name 'submit', and whose target is set to the validatable form. By default, the validatable form component
149+
will emit the event 'onSubmit', but you can actually override it if your form has multiple actions.
150+
151+
For instance, let's say that your form have one "save" and one "save and activate" button:
152+
153+
```html
154+
{{#validatable-form onPrimary=(action 'saveAndActivateUser') onSecondary=(action 'saveUser') as |form|}}
155+
// ...
156+
157+
<input {{action 'submit' 'onSecondary' target=form}} value="Save">
158+
<input {{action 'submit' 'onPrimary' target=form}} value="Save and activate">
159+
{{/validatable-form}}
160+
```
161+
162+
Your controller can now define the `saveAndActivateUser` and `saveUser` actions to properly handle them.
163+
148164
### Using with Ember-Data
149165
150-
Version 0.1.* do not offer any Ember Data integration as of today.
166+
Version 0.2.* do not offer any Ember Data integration as of today.
151167
152168
### Cookbook
153169

addon/components/validatable-form.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,6 @@ export default Ember.Component.extend({
3838
*/
3939
isSubmitting: false,
4040

41-
/**
42-
* Handle the submit event only if the form is valid
43-
*/
44-
submit() {
45-
if (!this.get('element').checkValidity()) {
46-
return false;
47-
}
48-
49-
this.set('isSubmitting', true);
50-
51-
new Ember.RSVP.Promise((resolve) => {
52-
resolve(this.get('onSubmit')());
53-
}).then(() => {
54-
this.set('isSubmitting', false);
55-
});
56-
57-
return false;
58-
},
59-
6041
actions: {
6142
/**
6243
* @param {string} inputName
@@ -70,6 +51,25 @@ export default Ember.Component.extend({
7051
}
7152

7253
this.rerender();
54+
},
55+
56+
/**
57+
* Handle the submit event only if the form is valid
58+
*/
59+
submit(eventName) {
60+
if (!this.get('element').checkValidity()) {
61+
return false;
62+
}
63+
64+
this.set('isSubmitting', true);
65+
66+
new Ember.RSVP.Promise((resolve) => {
67+
resolve(this.get(eventName || 'onSubmit')());
68+
}).then(() => {
69+
this.set('isSubmitting', false);
70+
});
71+
72+
return false;
7373
}
7474
}
7575
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-cli-html5-validation",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"description": "The default blueprint for ember-cli addons.",
55
"directories": {
66
"doc": "doc",

0 commit comments

Comments
 (0)