"Field is undefined" error with text input

271 Views Asked by At

I am using react native and to validate my inputs I am using validate.js, I used the code from this https://medium.com/@pavsidhu/validating-forms-in-react-native-7adc625c49cf and wired it up in my inputs using onChangeText and storing it in the state. But whenever any text input changes I get this error: "Can't find variable: field" and its coming from this code :

import {validatejs, validation} from 'validate.js';

export default function validate(fieldName, value) {
  //validate.js validates values an object
  //example - let form = {email: '[email protected]'}
  let formValues = {}; //creates an obj based on field name/field value
  formValues[fieldName] = value;


    // These next lines create a temporary form with the validation fields
    // e.g. var formFields = {
    //                        email: {
    //                         presence: {
    //                          message: 'Email is blank'
    //                         }
  //
  let formFields = {};
  formFields[fieldName] = validation[field];

  const result = validatejs(formValues, formFields); //compares formValues to formFields
    // this will return an error message if there is one

  if(result) {
    //return only that specific fields error if there is more than one.
    return result[field][0];
  }

  return null;
}

Could anybody help me understand how to fix this? I am new using validate.js

0

There are 0 best solutions below