文本输入时出现“字段未定义”错误

时间:2018-01-08 21:34:46

标签: javascript react-native validate.js

我正在使用react native并验证我的输入我正在使用validate.js,我使用了此https://medium.com/@pavsidhu/validating-forms-in-react-native-7adc625c49cf中的代码,并使用onChangeText将其连接到我的输入中并将其存储在状态中。但每当任何文本输入更改时,我都会收到此错误:“找不到变量:field”,它来自此代码:

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;
}

有人可以帮我理解如何解决这个问题吗?我是使用validate.js的新手

0 个答案:

没有答案