bietkul / react-native-form-builder

Handle your forms in a smart way
MIT License
121 stars 68 forks source link

customValidation example #21

Open anthonywebb opened 5 years ago

anthonywebb commented 5 years ago

I am not sure exactly how to use a "customValidation", I defined the function (along with a console.log statement to ensure it was arriving into the function), and then passed in into the form, but I dont seem to be calling the function as expected?

function validate(field) {
   console.log('validating',field);
   let error = false;
   let errorMsg = '';
   if (field.name === 'username' && !(field.value && field.value.trim())) {
     error = true;
     errorMsg = 'Username is required';
   }
   if (field.name === 'password' && !(field.value && field.value.trim())) {
     error = true;
     errorMsg = 'Password is required';
   }
   return { error, errorMsg };
}
<GenerateForm
            ref={(c) => {
              this.formGenerator = c;
            }}
            fields={fields}
            customValidation={validate}
          />