2.1.0 ā€¢ Published 3 months ago

@roqueform/constraint-validation-plugin v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Constraint validation API plugin for Roqueform

Integrates Roqueform fields with the Constraint validation API.

npm install --save-prod @roqueform/constraint-validation-plugin

Overview

šŸ”Ž API documentation is available here.

This plugin doesn't require any rendering framework. It subscribes to events that are dispatched by a DOM element passed to the ref method. To simplify the usage example, we're going to use the React integration.

import { FieldRenderer, useField } from '@roqueform/react';
import { constraintValidationPlugin } from '@roqueform/constraint-validation-plugin';

export const App = () => {
  const planetField = useField(
    { name: 'Mars' },
    constraintValidationPlugin()
  );

  return (
    <FieldRenderer field={planetField.at('name')}>
      {nameField => (
        <>
          <input
            type="text"
            pattern="Venus"
            // šŸŸ” Note that the input element ref is populated.
            ref={nameField.ref}
            value={nameField.value}
            onChange={event => {
              nameField.setValue(event.target.value);
            }}
            aria-invalid={nameField.isInvalid}
          />
          {nameField.validatedElement?.validationMessage}
        </>
      )}
    </FieldRenderer>
  );
};

Get the array of all invalid fields:

planetField.getInvalidFields();
// ā®• [planetField.at('name')]

Show an error message balloon for the first invalid element that is associated with this field or any of its child fields:

planetField.reportValidity();

Subscribe to the field validity changes:

planetField.on('change:validity', event => {
  event.target.validity;
  // ā®• ValidityState
});
2.1.0

3 months ago

2.0.0

3 months ago

1.0.1

9 months ago

1.0.0

1 year ago