validator is not a function error - react final form field arrays

1.2k Views Asked by At

I am trying to use the final-form-field-arrays component in my React App. When I try to use it, I get a "validator is not a function error ".

I traced it down to the runFieldLevelValidation function in final-form.es.js

var runFieldLevelValidation = function runFieldLevelValidation(field, setError) {
    var validators = field.validators;
    console.log(field);
    console.log(field.validators);
    var promises = [];
    var validatorKeys = Object.keys(validators);
    if (validatorKeys.length) {
      var error = void 0;
      Object.keys(validators).forEach(function (index) {
        var validator = validators[Number(index)];
        if (!validator.getValidator) {
        var errorOrPromise = validator(getIn(state.formState.values, field.name), state.formState.values);
        if (errorOrPromise && isPromise(errorOrPromise)) {
          promises.push(errorOrPromise.then(setError));
        } else if (!error) {
          // first registered validator wins
          error = errorOrPromise;
        }
        }
      });
      setError(error);
    }
    return promises;
  };

The line in question is

var errorOrPromise = validator(getIn(state.formState.values, field.name), state.formState.values);

After logging the fields (as you can see above), I found that the FieldArray field does not have the validator function itself, but instead, has a getValidator function. I monkey-patched it, and it is working, but I assume this should not be needed

My React Code in question is

 <Field name="name" component={RenderField} type="text" label="Name" />
                   <Field name="date" component={RenderField} type="date" text={moment().format('LL')} 
                   label="Date of Service" />
                   <Field name="time" type="select" component={RenderField} label="Time of Service" 
                   selectedOption={moment().format('k mm')} options={this.generateAppointmentTimes()} />
                          <FieldArray name="customers" >
         
        </FieldArray>

Any ideas on how to get this working without the monkey-patch?

1

There are 1 best solutions below

0
On BEST ANSWER

So, turns out I was using an incredibly old version of Final-Form, along with Final-Form-Arrays. I updated, and the error went away