I have a fully functional set of forms that are rendered with PHP and look something like this on the server-side:
<div id="app">
<form action="https://example.com" method="POST">
<div class="form-group">
<input type="text" name="name" value="<?php echo $_SESSION['name']['value'] ?? ''; ?>" />
<span class="error-message"><?php echo $_SESSION['name']['error'] ?? ''; ?></span>
</div>
<button type="submit" name="submit">Submit</button>
</form>
</div>
I want to know what the approach is to progressively enhance these forms with Vue.js. For example, intercepting form submission and performing an AJAX request, and using Vuelidate in addition to using all the other data binding perks of Vue.
If I were to just mount the instance right onto #app, then it would work, but any vue attributes I wrote in the HTML would end up making the HTML markup invalid if Vue somehow didn't load.
<div id="app">
<form @submit.prevent="submit" action="https://example.com" method="POST"
//...
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
</script>
// If the script never ran then @submit.prevent="submit" would be in the HTML markup.
My ultimate goal would be to have a form submitted with AJAX by Vue.js that is processed on the server-side with PHP, but also have server-side PHP rendering available in case Vue didn't load.
Do I need to use server-side rendering so that the server can determine whether to render Vue markup or regular HTML? Are there common approaches to this?