Skip to main content

Validation with yup

Our validation system is supporting any schema however we strongly recommend to check and use yup the documentation of yup library

There are two types of validations which we can trigger inside the form:

  • Client side validation based on yup schema
  • Backend validation based on submitted values and returned to the form
// any FieldStructure {
// other props in field structure,
validations?: unknown;
}

It is unknown since developers can use any validation library or just regular expressions.

Client side validation

Live Editor
Result

Backend side validation

To trigger backend validation, we need to return the same structure of fields as our form is sending to the server in Submit action, with information what was wrong. We will mock the backend validation return inside onSubmit method.

Live Editor
Result

Initial validation on render

We can trigger validation of fields after the render and before user will touch any field with just one property called showErrorsOnInitialRender.

Live Editor
Result

Validation based on value from different field

We have added support to validate field A based on the value of field B e.g. A and B are datepicker fields, and you want to check if date value in field B is not before date value of A.

To utilize this validation you need to apply in schema properties:

To field A

    associatedValidationBasedOnOtherFieldValues: {
fieldName: string;
useSamePath?: boolean;
validation: unknown;
}[]

This is applying additional validation schemas to the field

To field B

    arrayOfFieldsToRevalidateOnChange: {
fieldName: string;
useSamePath?: boolean;
}[]

This will allow field B to trigger validations in field A to check if something changed

useSamePath property is allowing to only put the field name inside the fieldName property without replicating the whole path to that field. You can spot the difference inside the schema.

It is super useful inside nested sections or array fields, since you can easily point to the nearest sibling field.

To see error message focus first input and blur it, or add property showErrorsOnInitialRender to FormWrapper like in example above (Initial validation on render)

Live Editor
Result