Strickland
  • Readme
  • Introduction
    • Validators
    • Validation
    • Validation Results
  • Extensibility
    • Validator Factories
    • Validation Context
    • Validation Result Props
    • Extensibility Pattern
    • formatResult
  • Built-In Validators
    • required
    • compare
    • min
    • max
    • range
    • minLength
    • maxLength
    • length
  • Composition
    • Arrays of Validators
      • every
      • all
      • some
    • Validating Array Elements
      • arrayElements
    • Validating Objects
      • objectProps
      • Advanced Object Validation
      • Nested Objects
      • Arrays of Objects
    • Composition Conventions
    • Composition and formatResult
  • Async Validation
    • Resolving Async Validation
    • Deferred Async Validation
    • Async Validator Arrays and Objects
    • Two-Stage Sync/Async Validation
    • Race Conditions
    • Automatic Race Condition Handling
    • Async Validation and formatResult
  • Form Validation
    • form
    • Async Form Validation
    • validateFields
    • emptyResults
    • updateFieldResults
  • Inspiration
  • Design Goals
  • Wrap-Up
  • Change Log
  • NPM
  • GitHub
Powered by GitBook
On this page
  • Usage
  • Removing Results

Was this helpful?

  1. Form Validation

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

const personValidator = form({
    firstName: [
        required(),
        length({minLength: 2, maxLength: 20})
    ],
    lastName: [
        required(),
        length({minLength: 2, maxLength: 20})
    ],
    birthYear: range({min: 1900, max: 2018})
});

let stanfordStrickland = {
    firstName: 'Stanford',
    lastName: 'Strickland',
    birthYear: 1925
};

let stanfordResult = validate(personValidator, stanfordStrickland);

let firstNameResult = {
    isValid: false,
    value: 'Stanford',
    message: 'The service does not allow a first name of "Stanford"'
};

stanfordResult = personValidator.updateFieldResults(
    stanfordResult,
    {firstName: firstNameResult}
);

/*
    stanfordResult = {
        form: {
            validationResults: {
                firstName: {
                    isValid: false,
                    value: 'Stanford',
                    message: 'The service does not allow a first name of "Stanford"'
                },
                lastName: {
                    isValid: true
                },
                birthYear: {
                    isValid: true
                }
            },
            validationErrors: [
                {
                    fieldName: 'firstName',
                    isValid: false,
                    value: 'Stanford',
                    message: 'The service does not allow a first name of "Stanford"'
                }
            ],
            isComplete: true
        }
    }
 */

Removing Results

To remove a field's results, provide null as the value of the field result.

stanfordResult = personValidator.updateFieldResults(
    stanfordResult,
    {firstName: null}
);
PreviousemptyResultsNextInspiration

Last updated 4 years ago

Was this helpful?