Skip to content

Commit 547f6a7

Browse files
committed
Updated README.md
1 parent 83f8575 commit 547f6a7

1 file changed

Lines changed: 40 additions & 17 deletions

File tree

README.md

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ customerFormValidation
4848
{ succeeded: true, type: "REQUIRED", key: "lastName", errorMessage: "" }
4949
]*/
5050
})
51-
.catch(error => {
51+
.catch((error) => {
5252
// handle unexpected errors
5353
});
5454
```
@@ -66,7 +66,7 @@ customerFormValidation
6666
console.log(validationResult.key); // "firstName"
6767
console.log(validationResult.errorMessage); // ""
6868
})
69-
.catch(error => {
69+
.catch((error) => {
7070
// handle unexpected errors
7171
});
7272

@@ -80,7 +80,7 @@ customerFormValidation
8080
console.log(validationResult.errorMessage);
8181
// "Please, fill in this mandatory field."
8282
})
83-
.catch(error => {
83+
.catch((error) => {
8484
// handle unexpected errors
8585
});
8686
```
@@ -114,15 +114,15 @@ const loginFormValidation = createFormValidation(loginValidationConstraints);
114114
Trigger field validation:
115115
```js
116116
loginFormValidation
117-
.validateField(null, 'login', 'jdoe', { onBlur: true})
117+
.validateField(null, 'login', 'jdoe', { onBlur: true })
118118
.then((validationResult) => {
119119
console.log(validationResult.succeeded); // false
120120
console.log(validationResult.type); // "EMAIL"
121121
console.log(validationResult.key); // "login"
122122
console.log(validationResult.errorMessage);
123123
// 'Please enter a valid email address.'
124124
})
125-
.catch(error => {
125+
.catch((error) => {
126126
// handle unexpected errors
127127
});
128128
```
@@ -143,7 +143,7 @@ const loginFormValidationConstraints = {
143143
{
144144
validator: Validators.pattern,
145145
customParams: {
146-
// It must belong to "lemoncode.net" domain
146+
// login must belong to "lemoncode.net" domain
147147
pattern: /\@lemoncode.net$/
148148
}
149149
}
@@ -152,7 +152,7 @@ const loginFormValidationConstraints = {
152152
};
153153
```
154154

155-
### Custom validators:
155+
### Custom validators
156156

157157
#### Field validator
158158

@@ -161,7 +161,7 @@ A field validator must return a `FieldValidationResult`. You can pass the entire
161161
```js
162162
function allowLowerCaseOnly(value, viewModel, customParams) {
163163
const isValid = value.toLowerCase() === value;
164-
const errorMessage = 'Field must be lowercase.`;
164+
const errorMessage = 'Field must be lowercase.';
165165
const validationResult = new FieldValidationResult();
166166
validationResult.succeeded = isValid;
167167
validationResult.errorMessage = isValid ? '' : errorMessage;
@@ -188,9 +188,9 @@ const signupValidationConstraints = {
188188
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:
189189

190190
```js
191-
function validateQuestions(...questions) {
191+
function validateQuestions(questions) {
192192
// All questions must be answered.
193-
return questions.every((question) => question.answer.trim().length);
193+
return questions.every((question) => question.answer.trim().length > 0);
194194
}
195195

196196
function questionsValidator(viewModel) {
@@ -213,13 +213,38 @@ const testFormValidation = createFormValidation(testFormValidationConstraints);
213213

214214
const viewModel = {
215215
questions: [
216-
{ id: 29, title: 'What method does merge two or more objects?', anwser: 'Object.assign' },
217-
{ id: 14, title: '', anwser: '' },
218-
{ id: 42, title: '', anwser: '' },
219-
{ id: 85, title: '', anwser: '' },
216+
{
217+
id: 29,
218+
title: 'What method does merge two or more objects?',
219+
anwser: 'Object.assign'
220+
},
221+
{
222+
id: 14, title: 'What character do you need to do string interpolation?',
223+
anwser: 'Backticks'
224+
},
225+
{
226+
id: 42,
227+
title: 'How to solve 0.1 + 0.2 === 0.3?',
228+
anwser: '+(0.1 + 0.2).toFixed(1)'
229+
},
230+
{
231+
id: 85,
232+
title: 'What month will new Date("2017", "04", "19").getMonth() produce?',
233+
anwser: 'May'
234+
},
220235
]
221236
};
222-
testFormValidation.validateForm()
237+
238+
testFormValidation
239+
.validateForm(viewModel)
240+
.then((validationResult) => {
241+
console.log(validationResult.succeeded); // true
242+
console.log(validationResult.formGlobalErrors) // []
243+
console.log(validationResult.fieldErrors); // []
244+
})
245+
.catch((error) => {
246+
// handle unexpected errors
247+
});
223248
```
224249

225250
## Documentation
@@ -268,8 +293,6 @@ Form validation is a complex issue, usually we can find solutions that cover the
268293

269294
- 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).
270295

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-
273296
## License
274297
[MIT](./LICENSE)
275298

0 commit comments

Comments
 (0)