npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-widgets

v1.1.3

Published

Auto generate form elements and validation

Downloads

1

Readme

Node Widgets

A NodeJS module for form generation with validation. This creates a form and form elements on server and return it to client using a json object called from client. You can edit the attributes in the json object and that reflects on the form elements.

Version 1.1.3

The supported form elements are text, password, hidden, number, email, url, textarea, select, checkbox, radio and validations are supported to text, password, number, email, url, taxtarea

Demo

  1. Clone the repository and go to demo folder and type npm install to install the necessary packages.
  2. Type node server. Then go to localhost:1234 or see the command prompt or terminal to know the port that app is running...

Or

Checkout this Live Demo

Setup

To install this module in your node project.

npm install node-widgets

So this install the dependencies and node-widgets module to your project and then you are good to go.

Implementation

Json Object

The Json object tells the library to create form and form elements that required.

This is a basic json object.

{
"form": "myForm",         //form name you need - optional (auto generated a form name if not)
"action": "/validate",    // method that form need to call - you can change this based on your routes/requirement
"method": "post",         // method type
"fields": {               // you need to mention this "fields"to create fields you required.
  "username": {           // this is the name of the element
    "label" : "Username",           // this is setted as label - optional
    "type": "text",                 // type of the element
    "class": "form-control",        // CSS class you need to render
    "required": true,               // required ? helps for validation : no validation takes place
    "minlen" : 10,                  // min length you allow
    "maxlen" : 20,                  // max length allow
    "msg": "Username is required"   // Error message that needs to render for required attribute (for min/max auto msg generated)
  },
   "age" : {              // Name of the element  
    "label" : "Age",
    "type" : "number",
    "class": "form-control",
    "required": true,
    "msg" : "Age is Invalid",
    "minval": 10,                   // Min value allowed
    "maxval" : 80                   // Max value allowed
  }
 }
}

So the common attributes for an input element are

  1. label
  2. type
  3. class
  4. value

Attributes that helps validation are

  1. required - Boolean
  2. minlen - Numeric
  3. maxlen - Numeric
  4. minval - Numeric
  5. maxval - Numeric
  6. match - String (name of the other element Ex: "match" : "password")

Function

There are only two functions in this library that takes care of form elements generation and validation.

toHTML

This function helps to create a form.


var nodeWidget = require('node-widgets');

var inputs = {
    "form": "myForm",
    "action": "/validate",
    "method": "post",
    "fields": {
      "username": {
        "label" : "Username",
        "type": "text",
        "class": "form-control",
        "required": true,
        "minlen" : 10,
        "maxlen" : 20,
        "msg": "Username is required"
      }
    }
  }

// create the form with json object
nodeWidget.toHTML(inputs, function(err, form){
  if(err) { throw err; }
  // form - is the template rendered HTML elements
});

validate

This method helps to validate the form, If form is valid it returns true with the complete form with data. if form is not valid then it returns false with updated form with error message and data..


var nodeWidget = require('node-widgets');
var bodyParser = require('body-parser');

var incomingData = req.body;

nodeWidget.validate(incomingData, function(err, valid, form){
  if(err) { throw err; }
  //valid  - boolean to know form is valid or not (true or false)
  //form - with updated err message and same elements to render
});