Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow resetField() to set (or keep) the "initialValue" associated to a field #4755

Closed
tbl0605 opened this issue May 27, 2024 · 6 comments
Closed

Comments

@tbl0605
Copy link

tbl0605 commented May 27, 2024

Hi,
first of all thank you for your fantastic form validation library.

Actually, you cannot keep the current initialValue associated to a field while using the resetField() method.

Would it be possible to make the resetField() method evolve to be able to do something like:

<Form ref="formRef">
    ...
    <Field
      as=""
      v-slot="slotData"
      v-model="myModel"
      ...
    >
        <my-form-input
          @myevent="
            slotData.resetField({
              value: 'XXX',
              initialValue: slotData.meta.initialValue
            });
            slotData.validate();
          "
          ...
        />
    </Field>
    ...
</Form>

And later be able to easily do $refs.formRef.resetForm() to restore all initial values?

Of course, my example is very simplified, but this new functionality would be very useful to me in my much more complex forms.

Fortunately, it looks like the changes needed are pretty straightforward if I look at the code in useField.ts:

  function resetField(state?: Partial<FieldState<TValue>>) {
    const newValue = state && 'value' in state ? (state.value as TValue) : initialValue.value;
    const newInitialValue = state && 'initialValue' in state ? (state.initialValue as TValue) : newValue;

    setState({
      value: deepCopy(newValue),
      initialValue: deepCopy(newInitialValue),
      touched: state?.touched ?? false,
      errors: state?.errors || [],
    });

    meta.pending = false;
    meta.validated = false;
    validateValidStateOnly();
  }

Thank you!

Thierry.

@logaretm
Copy link
Owner

Thanks for the kind words, I appreciate it.

It should be behaving like this right now AFAIK, we have tests for that. Can you please create a simple demo for this?

@logaretm logaretm added 🤔 needs reproduction This issue requires a demo and removed 🤔 needs reproduction This issue requires a demo labels May 28, 2024
@logaretm
Copy link
Owner

logaretm commented May 29, 2024

Sorry I misunderstood, so you want it to be able to set the initial value to possibly something different other than the current value?

This feels a bit weird to me, but you can do so in two steps:

  • Reset the field with the new initial value, also will set current value.
  • Set the current value with setValue right afterwards.

Does that work for you? Closing since it's not something I guess we should be doing, but feel free to explain your use-case and I will re-evaluate.

@logaretm logaretm closed this as not planned Won't fix, can't repro, duplicate, stale May 29, 2024
@tbl0605
Copy link
Author

tbl0605 commented May 30, 2024

Sorry I misunderstood, so you want it to be able to set the initial value to possibly something different other than the current value?

This feels a bit weird to me, but you can do so in two steps:

* Reset the field with the new initial value, also will set current value.

* Set the current value with `setValue` right afterwards.

Does that work for you? Closing since it's not something I guess we should be doing, but feel free to explain your use-case and I will re-evaluate.

Hi @logaretm,
thank you for your answer! :) Yes you understood correctly.

It would probably work, but setValue() is not (yet) accessible through the field slots. Something like this doesn't work yet (but it would be great if it could!):

<Form ref="formRef">
    ...
    <Field
      as=""
      v-slot="slotData"
      v-model="myModel"
      ...
    >
        <my-form-input
          @myevent="
            slotData.resetField({
              value: 'XXX'
            });
            slotData.setValue('YYY');
          "
          ...
        />
    </Field>
    ...
</Form>

So the real problem is probably that setValue() is not accessible through the field slot.

Tell me if I'm wrong: if I could use setValue() directly (through the field slot), it would no longer be necessary to use resetField() before setValue()?

@logaretm
Copy link
Owner

logaretm commented May 30, 2024

setValue() is not (yet) accessible through the field slots.

I suppose we can expose setValue since there is no harm in that. You can use handleChange as an alternative for now.

Tell me if I'm wrong: if I could use setValue() directly (through the field slot), it would no longer be necessary to use resetField() before setValue()?

Depends on what you want to do, if you want to reset the initial value to something entirely different other than the current initial value then yes. If you want to "keep" the initial value and only change the current (non-initial) value then yes you would only need setValue.

EDIT:

exposed in ae3772a which should be tagged soon.

@tbl0605
Copy link
Author

tbl0605 commented May 30, 2024

If you want to "keep" the initial value and only change the current (non-initial) value then yes you would only need setValue.

Yes that's exactly what I need :)

Thank you very much! :)

@SQLSourcerer
Copy link

I just realized I needed this, and here I come to find it was released just a few days ago. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants