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

form values will be covered by the initialValue value of usefield #4846

Closed
2 of 5 tasks
byronogis opened this issue Aug 26, 2024 · 2 comments · May be fixed by #4847
Closed
2 of 5 tasks

form values will be covered by the initialValue value of usefield #4846

byronogis opened this issue Aug 26, 2024 · 2 comments · May be fixed by #4847

Comments

@byronogis
Copy link

byronogis commented Aug 26, 2024

What happened?

// component A
// there is already have a form (by useForm), 
const {
  values,
  setValues,
} = useForm()

// and then use setValues to set a new value, 
setValues({
  name: 'x',
})

// component B
// create a new field (by useField, and have a avaliable initialValue y)
const {
  value
} = useField('name', undefined, {
  initialValue: 'y'
})

when the compoent B is dynamic create, the name will y not x

"vee-validate": "^4.13.2",
"vue": "^3.3.11",

Reproduction steps

See https://stackblitz.com/edit/puuk1z-imn8wg?file=src%2FApp.vue,src%2FInputText.vue&startScript=dev

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

https://stackblitz.com/edit/puuk1z-imn8wg?file=src%2FApp.vue,src%2FInputText.vue&startScript=dev

Code of Conduct

@byronogis
Copy link
Author

byronogis commented Aug 27, 2024

I carefully checked the record of #3429. Maybe the above situation is an acquiescence behavior?

I am currently adding a optional props forceValuein to Component B for solve current issue.

// component B
// create a new field (by useField, and have a avaliable initialValue y)


const props = defienProps<{
  forceValue?: any
  // ...
}>()

const {
  value
} = useField('name', undefined, {
  initialValue: props.forceValue || 'y'
})

@logaretm
Copy link
Owner

The initialValue on useField has priority over anything defined on the form. This is intended unfortunately.

There is no good way to satisfy both cases, it is a 50/50 toss on which behavior is correct depending on the case of course.

There is a long winded way to do it, you can try injecting the form context with inject or with useFormContext and check if the field value exists there before assigning it to initialValue.

const { values } = useFormContext();

const {
  value
} = useField('name', undefined, {
  initialValue: values['name'] ?? 'default',
});

This is something I'm considering to tackle in v5 but there is no right answer here so this is a bit tough to solve now.

@logaretm logaretm closed this as not planned Won't fix, can't repro, duplicate, stale Oct 19, 2024
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

Successfully merging a pull request may close this issue.

2 participants