availity-reactstrap-validation icon indicating copy to clipboard operation
availity-reactstrap-validation copied to clipboard

Is there a way to trigger form validation only onsubmit?

Open molly-ls opened this issue 7 years ago • 3 comments

I have tried to use validationEvent="onChange" but seems the validation still trigged onblur

molly-ls avatar May 18 '17 21:05 molly-ls

Validation is ran when the form initizes as well as when the event(s) in validationEvent are triggered. This is different from when the error messages display. AvField uses getInputState to determine to show the error message. getInputState relies in the field being touched and invalid. If you look at your inputs when the form initializes, the inputs are probably already invalid (you can tell by the av-invalid class). Blurring doesn't trigger the validation, but it does mark the field as touched, which in combination with the already invalid state triggers the AvFeedback to show. Sadly there is no way to change this currently. You would need to copy and implement your own AvField which can use your own logic to determine when the error message shows (probably based on dirty rather than touched. In the future we should probably allow this to be better configured. You may be asking "why" and "what the hell...", but validationEvent control just the validation function running and the make sense when you want to wait until blur to trigger the validation rather than every keystroke, but that means the input's validation state will not change/update until that event.

TheSharpieOne avatar May 19 '17 18:05 TheSharpieOne

Any update on this issue ? I'd like to have this feature too.

Tyube avatar Apr 27 '18 14:04 Tyube

If you want to validate onChange without having to wait for blurring.

  validate = _debounce((value, ctx, input, cb) => {
    // This will do the trick
    (value && !input.context.FormCtrl.isTouched(input.props.name)) &&
      input.context.FormCtrl.setTouched(input.props.name);

    cb(false);
  }, 300);

todotentei avatar Jun 28 '18 04:06 todotentei