Coffeekraken s-validator-component
Provide a nice and easy way to attach some validation rules to any particular form elements and decide how the reply messages will be displayed.
Features
- A bunch of pre-made validators like (required, min, max, range, maxlength, pattern, number, integer, color, email and url)
- Easy to register new validators
- Easy to translate the validators messages
- Handle checkboxes and radios validations
- Easy to customize how errors are displayed (even by validators types)
- Hook the form
checkValidity
method to reflect the validations - And more...
Table of content
- Demo
- Install
- Get Started
- Styling your validators
- Register a new validator
- The
apply
functions - Javascript API
- Sugar Web Components Documentation
- Browsers support
- Contribute
- Who are Coffeekraken?
- Licence
Install
npm install coffeekraken-s-validator-component --save
Get Started
First, import the component into your javascript file like so:
Then simply use it inside your html like so:
<!-- grab standard HTML validators from the target -->
THe
s-validator
component will grab the standards HTML validators from the target likemin
,max
,type="email"
,type="number"
,type="integer"
,type="url"
,type="color"
,required
,maxlength
andpattern
Styling your validators
Styling your validators become very simple. To help you style it depending on his state, some attributes will be present or not on the component itself as well as on the inputs that he handle. Here's the list:
On the component itself
- attribute
active
when an error message has to be displayed
On the inputs elements
- attribute
invalid
when the input is invalid - attribute
valid
when the input is valid - attribute
dirty
when the input has been touched by the user
Usage example
Here's how to apply a very simple styling to your validators
// styling the validator itself s-validator // styling the inputs input
Register a new validator
To register a custom validator, you just need to use the registerValidator
method.
// import the component// register a new validatorSValidatorComponent
The validate
and message
function takes each at least 1 argument. The targetFormElms
for the validate
one, and the corresponding raw validator message
for the message
one.
The other arguments are optional but are passed by following this pattern:
Explaination
In case of a validator that need two parameters like a range
type of one, it will be used like so:
The range
parameter will be splited by the :
separator and two additional parameters will be passed to the validate
and message
function like so:
SValidatorComponent
By doing this principle, your custom validators can have n
parameters. It's totally up to you.
apply
functions
The In order to display the error messages when needed, the component use the concept of apply
functions.
An apply
function is just a plain javascript function that apply the error message in the html and return an unapply
function that revert the apply
function actions.
The default apply
function that is used by the component set the error message inside the component itself, and his unapply
function just clear it with an innerHTML = '';
statement.
If this behavior suits your need, you can stop here, but you can totally set custom apply
and unapply
function to each validators. Here's how to do it:
SValidatorComponent;
apply
function
Override the default You can as well override the default apply
function like so:
SValidatorComponent
Browsers support
IE / Edge |
Firefox |
Chrome |
Safari |
---|---|---|---|
IE11+ | last 2 versions | last 2 versions | last 2 versions |
As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.
The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.
Contribute
This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:
Who are Coffeekraken
We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.
More on who we are
License
The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...