Acacha forms
Form objects pattern implementation for Javascript.
Installation
npm install acacha-forms --save
Or you can use unpkg in your html files:
Usage
See also examples folder with full code examples
ES6 imports
After package installation you could use this package using ES6 import:
Then you can create any form object using constructor, for example a Register User form:
let form = name: 'Sergi Tur' email: 'sergiturbadenas@gmail.com' password: '123456' password_confirmation: '123456' terms: 'true'
You can also pass a FormData object to constructor:
let form = document
And then use form methods like post to submit form:
form
See a full example using vue.js at examples/es6 folder.
Node.js require
After package installation you could use this package using require:
var AcachaForm = var API_URL = 'http://localhost:3000/users' var form = name: 'Sergi Tur' email: 'sergiturbadenas@gmail.com' password: '123456' password_confirmation: '123456' terms: 'true' form
See a full example using vue.js at examples/node folder.
Browser
...... I agree to the terms <!-- /.col --> Register <!-- /.col --> ...
Where register.js:
/* globals AcachaForm FormData $ */ /** * Submit register form. * * @param errors */ { // eslint-disable-line no-unused-vars const API_URL = 'http://localhost:3000/users' let form = document form } /** * Clear errors. * * @param errors */ { } /** * Show errors. * * @param errors */ { $ }
See a full example using vue.js at examples/browser folder.
Examples
See examples folder. Three examples are provided:
- browser: Example of how to use use this library in a simple web page.
- node: Example of use use this library using node.js require.
- es6: Example of use use this library using import es6 sintax (using vuejs framework).
All three examples requires to work a "server". You can execute the server provider al server folder which uses json-server.
Server
First remember to run the server example. Run the server typing:
cd examples/servernpm installnode server.js
Node.js
First remember to run the server example. Run the browser example typing:
cd examples/nodenpm installnode index.js
Browser
First remember to run the server example. Run the browser example typing:
cd examples/browsernpm installcd ..http-server .
An open browser/index.html file in your favourite browser.
es6
First remember to run the server example. Run the es6 (with vue.js) example typing:
cd examples/es6npm installnpm run dev
And open URL http://localhost:8080
Optional. Toastr error messages
You can active to show toastr errors activating second parameter on constructor:
let form = documenttrue
Then any error except 422 validation errors will show a Gritter/Toast with an error message.
IMPORTANT: Remember to add toastr.css to your project before using this option.
See browser example to see how to import toastr js and css.
About Form objects pattern
More info about this pattern at:
- http://crushlovely.com/journal/7-patterns-to-refactor-javascript-applications-form-objects/
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/19
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/20
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/21
Other similar packages or software
- https://github.com/laracasts/Vue-Forms
- https://github.com/edstevo/laravel-vue-form
- Laravel Spark : see more info about forms at docs.
Laracasts
This video series:
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/19
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/20
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/21
Inspired the creation of this package. Also Laravel Spark .
Resources
- Form Objects at acacha.org wiki: in Catalan Language
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/19
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/20
- https://laracasts.com/series/learn-vue-2-step-by-step/episodes/21
- https://github.com/laracasts/Vue-Forms
- https://github.com/edstevo/laravel-vue-form
- https://github.com/acacha/adminlte-laravel