-
Notifications
You must be signed in to change notification settings - Fork 6
Submit forms with fetch #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| 'form' => [ | ||
| 'success' => 'Form submitted successfully', | ||
| 'error' => 'Something went wrong, please try again later', | ||
| ], | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| return [ | ||
| 'form' => [ | ||
| 'success' => 'Formulier verzonden', | ||
| 'error' => 'Er ging iets fout, probeer het later opnieuw', | ||
| ], | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,28 @@ import '/public/vendor/statamic/frontend/js/helpers.js' | |
| Vue.prototype.Statamic = window.Statamic | ||
|
|
||
| export default { | ||
| props: ['initialData'], | ||
| props: { | ||
| initialData: { | ||
| type: Object, | ||
| required: true, | ||
| }, | ||
| translations: { | ||
| type: Object, | ||
| default: () => ({}), | ||
| }, | ||
| callback: { | ||
| type: Function, | ||
| }, | ||
| }, | ||
| data() { | ||
| return { | ||
| formData: this.initialData, | ||
| success: false, | ||
| error: false, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The developer can use these to show/hide validation on the frontend apart from the notify. |
||
| }; | ||
| }, | ||
| render() { | ||
| return this.$scopedSlots.default({ formData: this.formData }); | ||
| return this.$scopedSlots.default({ formData: this.formData, submit: this.submit }); | ||
| }, | ||
| mounted() { | ||
| let token = this.$root.csrfToken | ||
|
|
@@ -19,6 +33,28 @@ export default { | |
| if (csrfField && token && token !== 'STATAMIC_CSRF_TOKEN') { | ||
| csrfField.value = token | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| methods: { | ||
| async submit(event) { | ||
| event.preventDefault() | ||
|
|
||
| const response = await rapidezFetch(this.$el.action, { | ||
| method: this.$el.method, | ||
| body: new FormData(this.$el), | ||
| }) | ||
|
|
||
| if (response.ok) { | ||
| this.success = true | ||
| Notify(this.translations.success, 'success') | ||
| if (this.callback) { | ||
| await this.callback() | ||
| } | ||
| } else { | ||
| this.error = true | ||
| Notify(this.translations.error, 'error') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In some places in Rapidez we also log the error https://github.com/rapidez/core/blob/master/resources/js/fetch.js#L150 |
||
| } | ||
| }, | ||
| }, | ||
| } | ||
| </script> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a callback because in a project you might want to add some functionality like "closing a modal" or conversion tracking.