Basic uniforms usage

Imagine that we host an IT conference and want to make a list of guests. Obviously, we want to collect their first name and last name. Additionally, we can ask for their work experience, but that information is not required. Let's prepare a form for the guests, so they can sign up for our event, by using uniforms!

1. Install the required packages

To start using uniforms, we have to install three independent packages:

  1. Core
  2. Bridge
  3. Theme

In this example, we will use the JSONSchema to describe our desired data format and style our form using Semantic UI theme.

npm install uniforms
npm install uniforms-bridge-json-schema
npm install uniforms-semantic

Note: When using a themed package, remember to include correct styles! If you are willing to run this example by yourself, have a read on Semantic UI React's theme usage.

2. Start by defining a schema

After we've installed required packages, it's time to define our Guest schema. We can do it in a plain JSON, which is a valid JSONSchema instance:

const schema = {
title: 'Guest',
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
workExperience: {
description: 'Work experience in years',
type: 'integer',
minimum: 0,
maximum: 100
}
},
required: ['firstName', 'lastName']
};

As you can see, we've defined three properties - firstName and lastName, that are of string type, and workExperience, which is an integer, with a value between 0 and 100.

3. Then create the bridge

Now that we have the schema, we can create the uniforms bridge of it, by using the corresponding uniforms schema-to-bridge package. Creating the bridge instance is necessary - without it, uniforms would not be able to process form generation and validation. As we are using the JSONSchema, we have to import the uniforms-bridge-json-schema package.

import { JSONSchemaBridge } from 'uniforms-bridge-json-schema';

Now you may think that we can simply do:

// Wrong usage of the JSONSchemaBridge!
// You have to pass a validator!
const bridge = new JSONSchemaBridge(schema);

However, there's small caveat with using the JSONSchemaBridge. Because of its simplicity, JSONSchema doesn't provide any validation checkers, so in order to properly validate our submitted data, we need to manually define a validator, and that is required by the uniforms JSONSchemaBrigde constructor.

To manually create the validator, we will use the ajv package:

import Ajv from 'ajv';
const ajv = new Ajv({ allErrors: true, useDefaults: true });
function createValidator(schema) {
const validator = ajv.compile(schema);
return model => {
validator(model);
if (validator.errors && validator.errors.length) {
throw { details: validator.errors };
}
};
}
const schemaValidator = createValidator(schema);

Now that we have both the schema and the validator, we can create the uniforms bridge:

// Correct usage of the JSONSchemaBridge.
const bridge = new JSONSchemaBridge(schema, schemaValidator);

Just to recap, the whole GuestSchema.js file looks like this:

import Ajv from 'ajv';
import { JSONSchemaBridge } from 'uniforms-bridge-json-schema';
const ajv = new Ajv({ allErrors: true, useDefaults: true });
const schema = {
title: 'Guest',
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
workExperience: {
description: 'Work experience in years',
type: 'integer',
minimum: 0,
maximum: 100
}
},
required: ['firstName', 'lastName']
};
function createValidator(schema) {
const validator = ajv.compile(schema);
return model => {
validator(model);
if (validator.errors && validator.errors.length) {
throw { details: validator.errors };
}
};
}
const schemaValidator = createValidator(schema);
const bridge = new JSONSchemaBridge(schema, schemaValidator);
export default bridge;

4. Finally, use it in a form!

Uniforms theme packages provide the AutoForm component, which is able to generate the form based on the given schema. All we have to do now is to pass the previously created GuestSchema to the AutoForm:

import { AutoForm } from 'uniforms-semantic';
import React from 'react';
import GuestSchema from './GuestSchema';
export default function GuestForm() {
return <AutoForm schema={GuestSchema} onSubmit={console.log} />;
}

And that's it! AutoForm will generate a complete form with labeled fields, errors list (if any) and a submit button. Also, it will take care of validation and handle model changes.

SemanticMaterialBootstrap3Bootstrap4AntDUnstyled