Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lang/en/frontend.php
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',
],
];
8 changes: 8 additions & 0 deletions lang/nl/frontend.php
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',
],
];
42 changes: 39 additions & 3 deletions resources/js/components/FormConditions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Copy link
Copy Markdown
Member Author

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.

type: Function,
},
},
data() {
return {
formData: this.initialData,
success: false,
error: false,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

}
},
},
}
</script>
4 changes: 2 additions & 2 deletions resources/views/page_builder/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
@return
@endif

<form-conditions :initial-data='@json(Arr::pluck($form['fields'], 'default', 'handle'))' v-slot="{ formData }">
<form @attributes($form['attrs'])>
<form-conditions :translations="{{ json_encode(__('rapidez-statamic::frontend.form')) }}" :initial-data='@json(Arr::pluck($form['fields'], 'default', 'handle'))' v-slot="{ formData, submit }">
<form @submit.prevent="submit" @attributes($form['attrs'])>
@foreach($form['params'] as $name => $value)
<input type="hidden" name="{{ $name }}" value="{{ $value }}">
@endforeach
Expand Down
12 changes: 10 additions & 2 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,20 @@ public function boot()
->bootBuilder()
->bootSitemaps()
->bootStaticCaching()
->bootStack();
->bootStack()
->bootTranslations();

Vue::register();
Alternates::register();
}


protected function bootTranslations(): self
{
$this->loadTranslationsFrom(__DIR__ . '/../lang', 'rapidez-statamic');

return $this;
}

protected function bootBuilder(): self
{
config(['statamic.builder' => [
Expand Down