Skipping not required fields validation if the field is empty
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);
}
}
}
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
Pull request #34 fixes this.
Which has been merged to master on commit 3952e3230d81edbbb432e7eb7d50e4ae34a90ed4
I'm going to publish a new npm release tomorow ! Thx guys 👍
No problem. Thanks for reviewing and merging so quickly !
Looking forward to the release !
From: TibTib [email protected] Reply-To: perscrew/react-native-form-validator [email protected] Date: Wednesday, October 16, 2019 at 4:33 PM To: perscrew/react-native-form-validator [email protected] Cc: "Prasoon, Shikhar" [email protected], Comment [email protected] 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.