vue-form-generator icon indicating copy to clipboard operation
vue-form-generator copied to clipboard

Form not validated after changing model in onChanged

Open Anoesj opened this issue 5 years ago • 2 comments

I'm submitting a: [ x ] bug report

What is the current behavior? When changing values in the model in an onChanged callback, the form isn't validated.

Example: https://jsfiddle.net/Anoesj/k58zdLfs/47/ Change the value of the e-mail field. The password field should now change to a value that is too short. The form will not validate, until you either change the value of the password field manually, or hit the "Manually trigger validation" button.

What is the expected behavior? Because a value of the model changed, the form should have been validated afterwards.

What is the motivation / use case for changing the behavior? In complex forms, sometimes you need to keep the dataset clean by changing some values in the model when field values change. This should trigger form validation. Now, the only solution seems to be doing this:

onChanged: (model, newVal, oldVal) => {
  // Change password to default when e-mail value changes
  model.password = defaultPassword;

  // Manually trigger form validation in nextTick
  this.$nextTick(() => {
    this.$refs.vfg.validate();
  });
},

Environment:

  • Version: 2.3.4
  • Browser: probably all
  • Language: probably all

Anoesj avatar Apr 30 '19 07:04 Anoesj

Anyone? I'd like to know if this is intended to work. I have a lot of use cases where I need to reset some data when someone clicks on a certain option in a radio group. If the solution is to use a watch function instead of altering the model in an onChanged callback, so be it, but should this work or not?

Anoesj avatar May 24 '19 14:05 Anoesj

If the model is updated outside of VFG, then you will have to trigger validation manually.

For some use cases, you can probably use a custom VFG component field that has a watcher setup for the fields model, which can trigger validation.

zoul0813 avatar May 24 '19 15:05 zoul0813