Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Issue while setting the value of file/image input field with Vue Formulate

I'm trying to set the initial value of the image input field. The docs say that we need to provide an array of objects containing URLs. (link to docs)

I have set the value in the same format but it isn't working. Other fields ('text', 'email', etc) don't have this issue.

<template>
  <div class="w-full md:w-1/3">
    <FormulateForm
      :schema="formSchema"
      v-model="values"
      class="mt-6"
      @submit="submitHandler"
    />
    <button class="custom-button" type="button" @click="handlePopulateImage">
      Populate image
    </button>
  </div>
</template>

<script>
export default {
  name: "testForm",
  data() {
    return {
      values: {
        name: "",
        image: [],
      },
      formSchema: [
        {
          type: "text",
          name: "name",
          label: "Name",
          validation: "required",
        },
        {
          type: "image",
          name: "image",
          label: "Image",
          validation: "required",
        },
        {
          type: "submit",
          label: "Login",
        },
      ],
    };
  },
  methods: {
    handlePopulateImage() {
      // this works ๐Ÿ‘-----------------------------
      this.values.name = "Test Name (edited)";

      // this doesn't work ๐Ÿ˜ญ----------------------
      this.values.image = [
        {
          url:
            "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/John_Cena_July_2018.jpg/220px-John_Cena_July_2018.jpg",
          name: "test_name.png",
        },
      ];
    },
    submitHandler() {
      alert(`Thank you ${this.values.name}`);
    },
  },
};
</script>

<style scoped>
.custom-button {
  background-color: orange;
  padding: 1rem;
  border-radius: 5px;
}
</style>

Here is the link to the codesandbox: https://codesandbox.io/s/vue-formulate-demo-forked-y7288r?file=/src/components/TestForm.vue

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

I have no idea how Vue Formulate handles the initial value of images but re-rendering the component after initiliznig image urls does the trick.

<template>
  <div class="w-full md:w-1/3">
    <FormulateForm
      :schema="formSchema"
      v-model="values"
      class="mt-6"
      :key="randomKey"
      @submit="submitHandler"
    />
    <button class="custom-button" type="button" @click="handlePopulateImage">
      Populate image
    </button>
  </div>
</template>

<script>
export default {
  name: "testForm",
  data() {
    return {
      randomKey: Math.random(),
      values: {
        name: "",
        image: [],
      },
      formSchema: [
        {
          type: "text",
          name: "name",
          label: "Name",
          validation: "required",
        },
        {
          type: "image",
          name: "image",
          label: "Image",
          validation: "required",
        },
        {
          type: "submit",
          label: "Login",
        },
      ],
    };
  },
  methods: {
    handlePopulateImage() {
      // this works ๐Ÿ‘-----------------------------
      this.values.name = "Test Name (edited)";

      // this doesn't work ๐Ÿ˜ญ----------------------
      this.values.image = [
        {
          url:
            "https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/John_Cena_July_2018.jpg/220px-John_Cena_July_2018.jpg",
          name: "test_name.png",
        },
      ];
     this.randomKey = Math.random();
    },
    submitHandler() {
      alert(`Thank you ${this.values.name}`);
    },
  },
};
</script>

<style scoped>
.custom-button {
  background-color: orange;
  padding: 1rem;
  border-radius: 5px;
}
</style>

Codesandbox link: https://codesandbox.io/s/vue-formulate-demo-forked-c86uwt?file=/src/components/TestForm.vue

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!