Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form.FieldArray validation is not working #175

Open
Marcin-Kapusta opened this issue Dec 3, 2020 · 0 comments
Open

Form.FieldArray validation is not working #175

Marcin-Kapusta opened this issue Dec 3, 2020 · 0 comments

Comments

@Marcin-Kapusta
Copy link

Marcin-Kapusta commented Dec 3, 2020

I have following code. I exctracted only needed code for it. My model has name and array of simple fields. I just want to check if this array is not empty and raise validation error.

This is the example. Validation error for fields property is not working. Code is in Typescript. I expecting when I remove last field then I will have error message but there isn't any...

I also checked when such value with empty array I check using validate method on yup schema then it raises an error as expected. Because of that I think something is broken in React Formal.

What is really strange when min(2, 'Must have at least two fields') validation is applied to the fields then it works. Really strange behavior.

import React, { Fragment } from 'react';
import * as yup from 'yup';
import { ObjectSchema } from 'yup';
import Form from 'react-formal';

interface IField {
  id: number;
}
interface ITest {
  name: string;
  fields: IField[];
}

let cid: number = 0;
const initialData: ITest = {
  name: 'John',
  fields: [],
};

const fieldSchema = yup.object<IField>( { id: yup.number().required( 'Value is required' ) } ).required();
const schema: ObjectSchema<ITest> = yup.object<ITest>( {
  name: yup.string().required().default( '' ),
  fields: yup.array().of( fieldSchema )
    .required( 'Must exist' )
    .min( 1, 'Must have at least one field' ),
} ).required();
const FormalTest: React.FC = () => {
  return (
    <div>
      <Form<ObjectSchema<ITest>, ITest>
        schema={ schema }
        defaultValue={ initialData }
      >
        <Form.Field name="name" placeholder="Name" />
        <Form.Message for="name" />
        <Form.FieldArray<IField> name="fields">
          { ( values, arrayHelpers, meta ) => (
            <Fragment>
              { values.map( ( field, index ) => (
                <div key={ field.id }>
                  <Form.Field name={ `fields[${index}].id` } />
                  <button onClick={ () => arrayHelpers.remove( field ) }>Remove</button>
                </div>
              ) ) }
              <button onClick={ () => arrayHelpers.push( { id: cid+=1 } ) }>Add</button>
            </Fragment>
          ) }
        </Form.FieldArray>
        <Form.Message for="fields" />
      </Form>
    </div>
  );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant