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

Field type=checkbox does not work properly with v-model #3105

Closed
milochristiansen opened this issue Jan 4, 2021 · 7 comments
Closed

Field type=checkbox does not work properly with v-model #3105

milochristiansen opened this issue Jan 4, 2021 · 7 comments
Labels
🐛 bug Unintended behavior

Comments

@milochristiansen
Copy link

Versions

  • vee-validate: vee-validate@4.1.2
  • vue: vue@3.0.5

Describe the bug

When using <Field type="checkbox" v-model="checked"/> the value of checked will often be wrong. If it starts false, checking the box will set it to "on" (expected: true), unchecking will leave the value set to "on"! (expected: false) From this point on unchecking the box will set the value to "on" (expected: false) and checking it will set the value to "" (expected: true).

In the sandbox the behavior is a little different, there the v-model gets "stuck" to "on" no matter how many times you check or uncheck the box.

Replacing the Field tag with a simple input works exactly as expected.

To reproduce

  1. Create a empty Vue 3 project with Vee Validate installed.
  2. Add a Field with type="checkbox" and a v-model.
  3. Examine the value of the v-model as you check and uncheck the box.

Expected behavior

The checkbox should set the v-model to the proper boolean values instead of... whatever you want to call its current behavior.

Demo link

Vee-Validate Checkbox Bug

Desktop:

  • OS: Manjaro Linux
  • Browser: Chrome
  • Version: 86.0.4240.111
@logaretm logaretm added the 🐛 bug Unintended behavior label Jan 4, 2021
@logaretm
Copy link
Owner

logaretm commented Jan 4, 2021

Thank you for the detailed demo, it helped a lot. There was indeed a bug with the Field component without a wrapper Form in the case of checkboxes.

As a workaround you could wrap it in a virtual field like this:

https://codesandbox.io/s/vee-validate-checkbox-bug-forked-od4ud?file=/src/App.vue

I have fixed the issue and a release should be out shortly.

@milochristiansen
Copy link
Author

👍 Thanks! I worked around the issue for now (I didn't actually need to use validation for that field, but I was using the component everywhere for consistency), but it is nice to know I can revert my work around when the next update hits.

@milochristiansen
Copy link
Author

This issue appears to still be present in the latest version (4.2.4), albeit in changed form. Now the value is simply set to undefined and left at that. This behavior first appears in 4.1.4

(Wrapping a checkbox Field in another component (inside a Form) results in an issue very similar to this issue. I still need to investigate a bit more and write up a reproduction, but while working on that I revisited this issue and found out that it hadn't been fixed after all.)

@logaretm
Copy link
Owner

So now there are value and unchecked-value value props was introduced to the Field component, you would need to explicitly define them to get the correct behavior.

Feel free to build a reproduction and I will take another look, cheers!

@mas-iota
Copy link

So now there are value and unchecked-value value props was introduced to the Field component, you would need to explicitly define them to get the correct behavior.

Feel free to build a reproduction and I will take another look, cheers!

Hi, is this documented anywhere?

@demyan1411
Copy link

demyan1411 commented Nov 12, 2022

@DevMojahed
Copy link

DevMojahed commented Nov 6, 2023

Thank you for the detailed demo, it helped a lot. There was indeed a bug with the Field component without a wrapper Form in the case of checkboxes.

As a workaround you could wrap it in a virtual field like this:

https://codesandbox.io/s/vee-validate-checkbox-bug-forked-od4ud?file=/src/App.vue

I have fixed the issue and a release should be out shortly.

Thanks bro, it worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Unintended behavior
Projects
None yet
Milestone
No milestone
Development

No branches or pull requests

5 participants