formsy-angular
A form input builder and validator for Angular JS (NOT YET RELEASED)
- Background
- What you can do
- Install
- How to use
- API
- Validators
Background
I wrote an article on forms and validation with React JS, Nailing that validation with React JS, the result of that was an extension to React JS that I reimplemented in Angular JS.
The main concept is that forms, inputs and validation is done very differently across developers and projects. This extension to Angular JS aims to be that "sweet spot" between flexibility and reusability. Though Angular already has pretty good validation handling for forms it does not handle server requests and responses. It is also scoped to normal input elements. A formsy-input is just a value with validation linked to a form. How you choose to change that value does not matter, you can build anything you want.
What you can do
-
Build any kind of form input directive. Not just traditional inputs, but anything you want and get that validation for free
-
Add validation rules and use them with simple syntax
-
Use handlers for different states of your form. Ex. "submit", "error", "submitted" etc.
-
Server validation errors automatically binds to the correct form input
Install
- Download from this REPO and use globally or with requirejs
- Install with
npm install formsy-angular
and use with browserify etc. - Install with
bower install formsy-angular
It registers as the module 'formsy':
angular;
How to use
Formsy gives you a form straight out of the box
This code results in a form with a submit button that will POST to /users when clicked. The submit button is disabled as long as the input is empty (required) or the value is not an email (isEmail). On validation error it will show the message: "This is not a valid email".
This is an example of what you can enjoy building
angular ;
The template:
{{$message}}
Be as creative as you want and still get validation for free
angular ;
The template:
Increase ({{$value}})
Use it in a form:
So this is basically how you build your form elements. As you can see it is very flexible, you just have a few scope properties to help you reflect the states. As long as you update the value on the scope everything will validate automatically.
API
formsyProvider.defaults(options)
angular ;
Use defaults to set general settings for all your forms.
formsy-form
url
Will either POST or PUT to the url specified when submitted.
method
Supports POST (default) and PUT.
content-type
Supports json (default) and urlencoded (x-www-form-urlencoded).
Note! Response has to be json.
hide-submit
Hides the submit button. Submit is done by ENTER on an input.
submit-button-class
Sets a class name on the submit button.
cancel-button-class
Sets a class name on the cancel button.
button-wrapper-class
Sets a class name on the container that wraps the submit and cancel buttons.
success(serverResponse)
Takes a function to run when the server has responded with a success http status code.
submit()
Takes a function to run when the submit button has been clicked.
submitted()
Takes a function to run when either a success or error response is received from the server.
error(serverResponse)
Takes a function to run when the server responds with an error http status code.
formsy-input
So "formsy-input" is an attribute you use when creating your formsy directive to attach it to the form. It has to be set on the "top node" of the template. The first part of this section shows what attributes you can pass to your directive and the second part shows how you build the directive.
name
The name is required to register the form input directive in the form.
validations
An comma seperated list with validation rules. Take a look at Validators to see default rules. Use ":" to separate arguments passed to the validator. The arguments will go through a JSON.parse converting them into correct JavaScript types. Meaning:
Works just fine.
validation-error
The message that will show when the form input directive is invalid.
required
A property that tells the form that the form input directive value is required.
value
$value
To set a value inside your directive you can use two way data binding:
Or set the value manually:
angular ;
Flip color
$message
In your directive template:
{{$message}}
Will display the server error mapped to the form input directive or return the validation message set if the form input directive is invalid. If no server error and the form input directive is valid it does not display anything.
$valid
In your directive template:
{{$valid ? ':-)' : ':-('}}
Returns the valid state of the form input component.
$required
angular ;
{{label}} {{$required ? '*' : ''}}
Returns true if the required attribute has been passed.
$showRequired
In your directive template:
Lets you check if the form input directive should indicate if it is a required field. This happens when the form input directive value is empty and the required attribute has been passed, but its false if there is a validation error.
$showError
In your directive template:
Lets you check if the form input directive should indicate if there is an error. This happens if there is a form input directive value and it is invalid or if a server error is received.
formsyProvider.addValidationRule(name, ruleFunc)
An example:
angular ;
Another example:
angular ;
Validators
isValue
Returns true if the value is thruthful
isEmail
Return true if it is an email
isTrue
Returns true if the value is the boolean true
isNumeric
Returns true if string only contains numbers
isAlpha
Returns true if string is only letters
isLength:min, isLength:min:max
Returns true if the value length is the equal or more than minimum and equal or less than maximum, if maximum is passed
equals:value
Return true if the value from input component matches value passed (==).