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

InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable #3760

Closed
2 of 5 tasks
ikreb7 opened this issue Apr 29, 2022 · 5 comments
Closed
2 of 5 tasks
Labels
🐛 bug Unintended behavior

Comments

@ikreb7
Copy link

ikreb7 commented Apr 29, 2022

What happened?

I want to validate a input file field and validate this with the vee-validate. But If I choose a file I get this error message. I think the codesandbox example show it very well.

Reproduction steps

No response

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://codesandbox.io/s/mystifying-mclaren-orjrti?file=/src/components/HelloWorld.vue

Code of Conduct

@logaretm
Copy link
Owner

Are you sure? I can't see anything in the example related to inputs.

@logaretm logaretm added the 🤔 needs reproduction This issue requires a demo label Apr 29, 2022
@ikreb7
Copy link
Author

ikreb7 commented Apr 29, 2022

Are you sure? Here is the code example from codesandbox. You have to select a file. It doesn't throw errors if I use it without the <Field>-component.

<template>
  <div>
    <Form 
      as="form"
      class="needs-validation"
      :validation-schema="schema"
      novalidate=""
    >
      <div>
        <div class="row">
          <label for="formFile" class="col-sm-2 col-form-label">
            Upload field
          </label>
          <div class="col-9">
            <Field 
              name="formFile" 
              type="file" 
              v-slot="{ meta, field }"
            >            
              <input 
                v-bind="field"
                type="file" 
                class="form-control"
                :class="{
                  'is-valid': meta.valid && meta.touched,
                  'is-invalid': !meta.valid && meta.touched,
                }"
              >
            </Field>
            <ErrorMessage as="div" name="name" class="invalid-feedback" />
          </div>
        </div>
      </div>
      <div class="row m-5">
        <div class="col-auto">
          <input type="file" class="form-control" />
        </div>
      </div>
    </Form>
  </div>
</template>


<script>
import { markRaw } from 'vue';
import { Form, Field, ErrorMessage } from 'vee-validate';
import { object, mixed } from 'yup';

export default {
  name: "HelloWorld",
  components: {
    Form,
    Field,
    ErrorMessage,
  },
  props: {},
  data () {
    return {
      schema: markRaw(object().shape({
        formFile: mixed().required("This field is required"),
      })),
    }
  }

};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>

@logaretm logaretm removed the 🤔 needs reproduction This issue requires a demo label May 2, 2022
@logaretm
Copy link
Owner

logaretm commented May 2, 2022

Yea the example doesn't have that code. At least for me, anyways copied your code there and i can see the problem now.

v-bind="field" doesn't account for type=file at the moment, since file inputs cannot have two-way binding. As a workaround you can do the following:

<Field 
  name="formFile" 
  type="file" 
  v-slot="{ meta, handleChange }"
>            
  <input 
    @change="handleChange"
    type="file" 
    class="form-control"
    :class="{
      'is-valid': meta.valid && meta.touched,
      'is-invalid': !meta.valid && meta.touched,
    }"
  >
</Field>

I will keep this open to think about if vee-validate should handle this case if you pass type=file to the Field component or not since it might affect custom file input components as well.

@logaretm logaretm added the ✨ enhancement a "nice to have" feature label May 2, 2022
@ikreb7
Copy link
Author

ikreb7 commented May 2, 2022

Thank your very much! It works :)

Sounds good, if you change the binding or mention this in the documentation.

@logaretm logaretm added 🐛 bug Unintended behavior and removed ✨ enhancement a "nice to have" feature labels May 7, 2022
@logaretm
Copy link
Owner

logaretm commented May 7, 2022

I decided to mark this as a bug since clearly some conditions didn't work as expected. Fixed in 3c76bb2

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
Development

No branches or pull requests

2 participants