Skip to content

Instantly share code, notes, and snippets.

@oliuz
Forked from calebporzio/VueForm.vue
Created August 18, 2019 00:00
Show Gist options
  • Save oliuz/ee19ae982bf3d6e24fa506f3fc3d0e0e to your computer and use it in GitHub Desktop.
Save oliuz/ee19ae982bf3d6e24fa506f3fc3d0e0e to your computer and use it in GitHub Desktop.
A Vue component to include Laravel's CSRF Token within form tag
// Usage:
// <vue-form method="PUT">
// <input type="text" name="email">
// <input type="submit">
// </vue-form>
<template>
<form :method="method.toUpperCase() == 'GET' ? 'GET' : 'POST'">
<input-hidden :value="csrfToken" name="_token"/>
<input-hidden
v-if="['GET', 'POST'].indexOf(method.toUpperCase()) === -1"
:value="method"
name="_method"
/>
<!--
This hidden submit button accomplishes 2 things:
1: Allows the user to hit "enter" while an input field is focused to submit the form.
2: Allows a mobile user to hit "Go" in the on-screen keyboard to submit the form.
-->
<input type="submit" class="absolute invisible z-0">
<slot/>
</form>
</template>
<script>
export default {
props: { method: { default: 'POST' }},
data() { return { csrfToken: null }},
created() {
this.csrfToken = document.querySelector('meta[name="csrf-token"]').content
},
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment