Laravel Form Javascript Object
This is a package for handling forms using an object oriented approach.
Installation
In your console run:
# Using npm npm install laravel-form-js # Using yarn yarn add laravel-form-js
Usage
Example using Vue
<template> <form @submitprevent="submit"> <input type="text" name="name" v-model="form.name"> <div v-text="form.errorsFor('name')"></div> <input type="text" name="email" v-model="form.email"> <div v-text="form.errorsFor('email')"></div> </form></template><script>let Form = ; props: url: type: String required: true method: type: String required: true user: { return name: '' email: '' ; } { return form: Form // If no method is given, the default is post. } methods: { thisform ; } </script>
Note
This library works with the validation of laravel >= 5.5, because now the errors are given like this:
errors: name: email:
Creating a form
There are two ways to create a new form:
Form;// Ordata method = 'post' clearAfterResponse = false;
Available Methods
The methods you can call from the form object are:
form.data(dataType)
: Get the form data in a given format. Supported data types:json
,form-data
;form.errorsFor(field)
: Return the errors for the given field. If there are not errors it returnsnull
.form.setMethod(method)
: Change the form method. Supported methods:post
,put
,patch
.form.submit(url, dataType = 'form-data')
: Submit the form to the given url using the form method and passing the data in the given type. Supported data types:json
,form-data
;form.reset()
: Reset the form to the original data.