Forminator
A simple and flexible Javascript form generator and validator. Based on forms.
Installation
Use the package manager npm to install forminator.
npm install --save forminator
Usage
In this example we're using Express and ejs as template engine.
test.js
const forms = ;const fields = formsfields;const validators = formsvalidators; var test_form = forms; moduleexports = test_form;
index.js
const TestForm = ; var test_form = ; // GET Indexapp; // POST Indexapp;
index.ejs
Hello World! <%- form.fields.test.label.render() %> <%- form.fields.test.render() %> Attend
This would produce:
Hello World! Hello There! Attend
There are 3 ways to validate your form:
/** * This is the most efficient way. * Pro: A new form instance is only generated when 'handle' method is called. * Con: The 'submitted' form from handle won't copy any new attributes that you added * or modified after the first form is generated. * * If that is not a problem, then this is the best method for you. */ const TestForm = ;var test_form = ; // This won't be in the 'submitted' formtest_formfieldstest_class = 'form-control' app; app;
/** * This is the less headache way. * Pro: Customizable form. * Con: A new form instance is generated at least twice (at 'GET' + 'POST' route). * (But, I think you already expected this) * * If you need to do something with the form instance first (ex: dynamically generated * choices for selectField), then use this method. */ const TestForm = ; { var form = ; formfieldstest_class = 'form-control'; return form;} app; app;
/** * Exactly the same as the second one, but with async. * Pro: Clean looking code. * Con: You need to use an async handler (I use express-async-handler). * * This method is recommended for cleaner looking code. */ const asyncHandler = const TestForm = ; { var form = ; formfieldstest_class = 'form-control'; return form;} app; app;
You can add attributes to your tags like this:
var test_form = forms;
const TestForm = ;var form = ;formfieldstest_class = 'form-control';
formfieldstest;
The third method won't accept 'id', 'name', and 'type' attributes. This is intentional.
Note that the attributes only accept string or boolean as their value. So, make sure to convert your numbers into strings first before inserting them.
Be careful when adding attributes to your fields.
Available Fields
- labelField
- inputField
- selectField
- textAreaField
var test_form = forms;
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
README created using Make a README