0

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: 'john@bob.com'}
  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

Perniferous
  • 513
  • 6
  • 19
  • Are you hoping that JS can guess what you mean if you say `formFields[fieldName] = validation[field];`, but you have not defined `field` anywhere? ;) – Gerard van Helden Jan 08 '18 at 21:37
  • I know this is what I am confused about. The code is from that medium article and thats what he used. – Perniferous Jan 08 '18 at 21:40

0 Answers0