christianalfoni / formsy-react

A form input builder and validator for React JS
MIT License
2.6k stars 436 forks source link

Adding a validation rule for isRequired? #352

Open JohnBlazek opened 8 years ago

JohnBlazek commented 8 years ago

I'm looking for a way to check and see if a field I'm editing is both required and matches a certain condition within a validation rule. I'm noticing there is no validation rule that checks to see if a field is required. I would be looking to see if the field is required from the tag itself...

something like:

Formsy.addValidationRule('equalsFieldAndRequired', function (values, value, field) { return value === values[field] && this.isRequired();

});

Hope this makes sense!

lennardschutter commented 7 years ago

I added a custom rule for this for the time being because I did not want to fall back to the HTML5 validation.

Formsy.addValidationRule('isRequired', (values, value) => {
  return value && value.toString().trim().length;
});
lennardschutter commented 7 years ago

@Semigradsky Is this not what the isDefaultRequiredValue() validation is for? Although it is missing some cases (whitespace, null)..

krazi3 commented 7 years ago

@lennardschutter i had the same issue with null cases being let through

lennardschutter commented 7 years ago

I think I ran in to the same issue this week. Might look at creating a pull request.

On 7 Feb 2017, at 7:02 pm, Awwab Ul Haq notifications@github.com wrote:

@lennardschutter i had the same issue with null cases being let through

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.