Strickland
Search…
updateFieldResults
In some scenarios, validation results need to be produced outside the flow of calling into Strickland to perform validation. For example, an API call might perform server-side validation and return validation results that need to be populated into your application's validation result state.
To assist with such scenarios, Strickland's form validator offers an updateFieldResults helper function that can update existing validation results with new field results.

Usage

1
const personValidator = form({
2
firstName: [
3
required(),
4
length({minLength: 2, maxLength: 20})
5
],
6
lastName: [
7
required(),
8
length({minLength: 2, maxLength: 20})
9
],
10
birthYear: range({min: 1900, max: 2018})
11
});
12
13
let stanfordStrickland = {
14
firstName: 'Stanford',
15
lastName: 'Strickland',
16
birthYear: 1925
17
};
18
19
let stanfordResult = validate(personValidator, stanfordStrickland);
20
21
let firstNameResult = {
22
isValid: false,
23
value: 'Stanford',
24
message: 'The service does not allow a first name of "Stanford"'
25
};
26
27
stanfordResult = personValidator.updateFieldResults(
28
stanfordResult,
29
{firstName: firstNameResult}
30
);
31
32
/*
33
stanfordResult = {
34
form: {
35
validationResults: {
36
firstName: {
37
isValid: false,
38
value: 'Stanford',
39
message: 'The service does not allow a first name of "Stanford"'
40
},
41
lastName: {
42
isValid: true
43
},
44
birthYear: {
45
isValid: true
46
}
47
},
48
validationErrors: [
49
{
50
fieldName: 'firstName',
51
isValid: false,
52
value: 'Stanford',
53
message: 'The service does not allow a first name of "Stanford"'
54
}
55
],
56
isComplete: true
57
}
58
}
59
*/
Copied!

Removing Results

To remove a field's results, provide null as the value of the field result.
1
stanfordResult = personValidator.updateFieldResults(
2
stanfordResult,
3
{firstName: null}
4
);
Copied!
Last modified 1yr ago
Copy link