You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LcFormValidation is a small JavaScript library that provides you form validation. Validate your viewmodel either on client or server side with Node.js.
5
+
It is third party / framework agnostic, so you can easily add it in your stack, but it integrates quite well with libraries like React / Redux.
4
6
5
-
* Heavily based on JavaScript (no html attributes annotations).
6
-
* Full async, all validations are processed as async.
7
-
8
-
lc-FormValidation is third party / framework agnostic, although it will integrate quite well with libraries like React / Redux.
7
+
- Heavily based on JavaScript (no html attributes annotations).
8
+
- Full async, all validations are processed as async using native [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) (LcFormValidation already gives you a polyfill for browsers that do not support promises).
9
9
10
10
# Why another form library? #
11
11
@@ -17,161 +17,43 @@ Form validation is a complex issue, usually we can find solutions that cover the
17
17
18
18
* Validations are tightly coupled to e.g. directives or markup is not easy to reuse this validation code in e.g. server side (universal javascript).
19
19
20
-
# Approach #
21
-
22
-
In a form validation we can find features / code that can be placed in a base class (baseFormValidation):
23
-
24
-
* ValidateField: Validates a single field (e.g. call this when controls content change or the control looses the focus).
25
-
* ValidateForm: Validates the whole form (all fields, plus global validations).
26
-
27
-
28
-
Then we can inherit from that base class and define our own specific form validations:
29
-
30
-
* Add in the constructor of the inherited class the mappings between form field Id's and form and entity field Id, plus defining the validations per field to be executed.
31
-
32
-
The idea is to encapsulate all the common functionality in a base class, and create independent classes that will inherit from this base class to implement the validation of each individual form.
33
-
34
-

35
-
36
-
By following this approach:
37
-
38
-
* We encapsulate all the generic validation plumbing in a base class.
39
-
* We can implement real world form validation in the specific classes.
40
-
* We can benefit from external validation libraries (validate emails, url, iban ...), wihtout having to implement additional scaffolding (e.g. create a directive for this validation).
41
-
* We can easily test (plain javascript framework agnostic):
42
-
* The formValidationBase.
43
-
* Each specific form validation class.
44
-
* The generic validator helpers.
45
-
* We can just run this javascript code in the client and serverside (e.g. node solution) ** (NOTE) We expect to create a sample showing how this could be implemented soon.
46
-
47
-
48
-
# FormBaseValidation + Sample Form #
49
-
50
-
FormBaseValidation implementation (already implemented by the library):
51
-
52
-
53
-
```
54
-
export class BaseFormValidation {
55
-
_validationEngine: IValidationEngine;
56
-
57
-
constructor() {
58
-
this._validationEngine = new ValidationEngine();
59
-
}
60
-
61
-
public validateField(vm: any, key: string, value: any, filter : any = consts.defaultFilter): Promise<FieldValidationResult> {
A simple quiz with three options where there has to be at least one checked option to pass the validation (custom global validation).
45
+
[View source code](./samples/react/02%20QuizForm)
109
46
110
-
To install the stable version:
111
47
112
-
```
113
-
npm install --save lc-form-validation
114
-
```
48
+
### jQuery examples
115
49
116
-
This assumes you are using [npm](https://www.npmjs.com/) as your package manager.
117
-
If you don’t, you can [access these files on unpkg](https://unpkg.com/lc-form-validation/), download them, or point your package manager to them.
50
+
#### Shopping form
118
51
119
-
Prerequisites: In order to get these examples up and running you will have to get installed typings and webpack node modules globally.
52
+
A little shopping form where the user has to select a product with a version and optionally apply a discount code and enter its NIF. Validations applied:
120
53
121
-
```
122
-
npm install typings -g
123
-
```
124
-
125
-
If you want to start from the source code, follow this steps:
126
-
127
-
First download the source code from Git.
128
-
129
-
* Navigate to the lib folder (Note: this process will be less painful and added to preinstall when the following [bug](https://github.com/npm/npm/issues/10379) gets fixed on npm.
130
-
131
-
* Execute npm install
132
-
133
-
```
134
-
lcFormValidation\lib> npm install
135
-
```
136
-
137
-
* Execute build
138
-
139
-
```
140
-
lcFormValidation\lib> npm run build
141
-
```
142
-
143
-
* Navigate to one the samples folders, e.g. "samples/react/00 Simple Form".
144
-
145
-
```
146
-
lcFormValidation> cd "samples/react/00 SimpleForm"
147
-
```
148
-
149
-
* Execute npm install (this is connected to the local lib folder).
* Open your favourite browser and navigate to http://localhost/8080
162
-
163
-
If you want to run the unit tests that are located under the lib folder from the command line you will need to install karma-cli globally (npm install karma-cli -g), in order to run them, type from your command line "karma start".
164
-
165
-
# Samples forms implemented #
166
-
167
-
Samples running with React + Redux:
168
-
* Basic login.
169
-
* Signup form (including async validation against GitHub user api).
170
-
* Quiz form.
54
+
- The brand and product fields are mandatory (required validator).
55
+
- The discount code is optional but needs to have a valid format if fulfilled (pattern validator).
56
+
- NIF field is mandatory with a valid format (required validator + custom validator
We are looking for contributors to implement samples and support for libraries such as Angularjs, Ember... if you would like to cooperate don't hesitate contacting us.
173
-
174
-
# Future Enhancements #
175
-
176
-
* Allow connecting with array fields / table like scenario.
177
-
* Allow this to be easily be used outside the context of a web form (e.g. rest api server side)
0 commit comments