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
@@ -7,53 +7,271 @@ It is third party / framework agnostic, so you can easily add it in your stack,
7
7
- Heavily based on JavaScript (no html attributes annotations).
8
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
-
#Why another form library? #
10
+
## Quick start
11
11
12
-
Form validation is a complex issue, usually we can find solutions that cover the simple scenarios and are focused on building RAD development just by adding some attributes / annotations to given fields or HTML fields (e.g. input), the disadvantage that we have found to this approach:
> Not passing `eventsFilter` as third parameter results in `{ onChange: true }` by default.
130
+
131
+
### Passing extra arguments
132
+
133
+
You can pass custom parameters that will be injected into the validator using the `customParams` property:
134
+
135
+
```js
136
+
constloginFormValidationConstraints= {
137
+
fields: {
138
+
login: [
139
+
// Mandatory field
140
+
{ validator:Validators.required },
141
+
// It has to be a valid email address
142
+
{ validator:Validators.email },
143
+
{
144
+
validator:Validators.pattern,
145
+
customParams: {
146
+
// It must belong to "lemoncode.net" domain
147
+
pattern:/\@lemoncode.net$/
148
+
}
149
+
}
150
+
]
151
+
}
152
+
};
153
+
```
154
+
155
+
### Custom validators:
156
+
157
+
#### Field validator
158
+
159
+
A field validator must return a `FieldValidationResult`. You can pass the entire `viewModel` if you need to validate a field based on other fields:
13
160
14
-
* There are different approaches for each scenario: sometimes you have to add some tweaking for async validations, some other take care of special scenarios (like validations that depends on more than one field).
* Usually you can easily unit test one validation (directive / annotation), but testing the whole form is a complex task (directives are coupled to e.g. HTML).
170
+
return validationResult;
171
+
}
172
+
```
17
173
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).
174
+
Apply inside your constraints:
19
175
20
-
## Overview
176
+
```js
177
+
const signupValidationConstraints = {
178
+
fields: {
179
+
username: [
180
+
{ validator: allowLowerCaseOnly }
181
+
]
182
+
}
183
+
};
184
+
```
185
+
186
+
#### Global form validator
187
+
188
+
A global validator will accept the entire viewModel and return a `FieldValidationResult`. Put your global validators inside `global` property of your validation constraints. It is useful, e.g., for validating dynamic properties:
#### Quiz form ([ES6](), [TypeScript](./samples/react/02%20QuizForm))
43
248
44
249
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)
46
-
47
250
48
251
### jQuery examples
49
252
50
-
#### Shopping form
253
+
#### Shopping form ([ES6](./samples/jquery/Shopping%20form))
51
254
52
255
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:
53
256
54
257
- The brand and product fields are mandatory (required validator).
55
258
- The discount code is optional but needs to have a valid format if fulfilled (pattern validator).
56
259
- NIF field is mandatory with a valid format (required validator + custom validator
Form validation is a complex issue, usually we can find solutions that cover the simple scenarios and are focused on building RAD development just by adding some attributes / annotations to given fields or HTML fields (e.g. input), the disadvantage that we have found to this approach:
264
+
265
+
- There are different approaches for each scenario: sometimes you have to add some tweaking for async validations, some other take care of special scenarios (like validations that depends on more than one field).
266
+
267
+
- Usually you can easily unit test one validation (directive / annotation), but testing the whole form is a complex task (directives are coupled to e.g. HTML).
268
+
269
+
- 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).
58
270
59
271
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.
272
+
273
+
## License
274
+
[MIT](./LICENSE)
275
+
276
+
# About Lemoncode
277
+
We are a team of long-term experienced freelance developers, established as a group in2010. We specialize in Front End technologies and .NET. [Click here](http://lemoncode.net/services/en/#en-home) to get more info about us.
0 commit comments