perscrew / react-native-form-validator

React native library to validate form fields
127 stars 55 forks source link

Skipping not required fields validation if the field is empty #25

Open artshevtsov opened 5 years ago

artshevtsov commented 5 years ago

I fixed _checkRules function a little bit - if the field is empty and not required there should be no errors, for example that email is not valid or something else

// Method to check rules on a spefific field
  _checkRules(fieldName, rules, value) {
    + const isRequired = !!rules.required;
    + if (!isRequired && !value.length) {
    +  //Nothing to check
    +  return;
    +}
    for (const key of Object.keys(rules)) {
      const isRuleFn = (typeof this.rules[key] == "function");
      const isRegExp = (this.rules[key] instanceof RegExp);
      if ((isRuleFn && !this.rules[key](rules[key], value)) || (isRegExp && !this.rules[key].test(value))) {
        this._addError(fieldName, key, rules[key], isRuleFn);
      }
    }
  }
shikhar91939 commented 5 years ago

We are encountering the same issue. The form requires to fill in a field that has other rules but does not have the required rule as true.

The solution you proposed works as expected. I will be creating a PR for it

shikhar91939 commented 5 years ago

Pull request #34 fixes this.

Which has been merged to master on commit 3952e3230d81edbbb432e7eb7d50e4ae34a90ed4

perscrew commented 5 years ago

I'm going to publish a new npm release tomorow ! Thx guys 👍

shikhar91939 commented 5 years ago

No problem. Thanks for reviewing and merging so quickly !

Looking forward to the release !

From: TibTib notifications@github.com Reply-To: perscrew/react-native-form-validator reply@reply.github.com Date: Wednesday, October 16, 2019 at 4:33 PM To: perscrew/react-native-form-validator react-native-form-validator@noreply.github.com Cc: "Prasoon, Shikhar" sprasoon@quickbase.com, Comment comment@noreply.github.com Subject: Re: [perscrew/react-native-form-validator] Skipping not required fields validation if the field is empty (#25)

I'm going to publish a new npm release tomorow ! Thx guys 👍

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/perscrew/react-native-form-validator/issues/25?email_source=notifications&email_token=AB3E6WC553DUV4OGUG26XJTQO53ATA5CNFSM4GQLOHM2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBN26XQ#issuecomment-542879582, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AB3E6WBDMXI65XHOYRQE6NTQO53ATANCNFSM4GQLOHMQ.

This message and its contents are confidential. If you received this message in error, do not use or rely upon it. Instead, please inform the sender and then delete it. Thank you.