Encountering a problem when trying to assign a value to a file/image input field using Vue Formulate

Having trouble setting the initial value for the image input field? The documentation suggests providing an array of objects with URLs (link to docs).

I've followed the same format for other fields like 'text' and 'email', which work fine.

<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>

Check out the codesandbox link for more details: https://codesandbox.io/s/vue-formulate-demo-forked-y7288r?file=/src/components/TestForm.vue

Answer №1

Vue Formulate's handling of initial image values is a bit fuzzy, but triggering a re-render after initializing image URLs seems to do 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>

View the sandbox here: https://codesandbox.io/s/vue-formulate-demo-forked-c86uwt?file=/src/components/TestForm.vue

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing incremental value for a dropdown in JavaScript and php when integrating Ajax requests

I am currently working on implementing multiple dynamic drop-down options and attempting to set an incremental value when the user clicks the add button (utilizing the .append function in JavaScript) for both the drop-down options in JavaScript and PHP whi ...

Error encountered in Vue code, lacks default export on Editor Terminal

I'm not experiencing any issues in the browser, I am getting the desired output. However, why does this keep showing up in the editor terminal? Any assistance would be greatly appreciated. Error - No Default Export: The module "/vue3/src/components/ ...

What is the best method for performing cross-domain queries utilizing ajax and jsonp?

When attempting to send an ajax request to a specific URL, I encountered an error. Below is the code I used: $.ajax({ url: "http://webrates.truefx.com/rates/connect.html?q=ozrates&c=EUR/USD&f=csv&s=n", dataType : 'jsonp', ...

Unresolved error causing promise to throw an exception

My code is causing an error when resolve is called: Possibly unhandled Error: undefined at Promise$_rejecter (c:\projects\Test\promiseftp\node_modules\bluebird\js\main\promise.js:602:58) at WriteStream.<a ...

How to position div in the middle of Electron/VUE application's screen?

I am struggling to center a container div on the screen. The technology stack I am using includes Electron, VUE, HTML, and CSS. Here is the code snippet I have attempted: <template > <div class="login background" style="height:100%" > ...

Exploring JSON and jQuery to Address Filtering Challenges

Excuse the interruption, but I need some assistance with my filters. Below is the code I'm currently working on; however, none of my attempts have been implemented yet (the dropdown menu and checkboxes remain non-functional) to make it easier for you ...

capturing webpage content with javascript for use in a screenshot

Is there a way to capture a screenshot of a webpage using JavaScript and utilize the canvas tag for this purpose? I attempted to use the html2canvas plugin in the past, but found it lacking in power. I would like to achieve this without relying on extern ...

What is the proper way to reference a computed symbol that has been inherited as a function in an extended class

As a newcomer to Typescript, my understanding of the code might be lacking. I am currently working with the Klasa framework, which is built on top of Discord bot framework using Discord.js. The framework recently introduced plugin functionality and there a ...

Is there a way to troubleshoot and adjust this regex to accurately replace asterisks within words?

I've been experimenting with regex. I came up with a regex pattern that converts * into <em>, similar to Markdown: el = el.replace(/\*\b/g, '<em>') el = el.replace(/\b\*|(\.|\,|\?|\!|&bsol ...

Show loading GIF on the screen and hide it once the script has finished loading

I have implemented lazy loading for my Facebook comments plugin, but I am facing an issue where the plugin takes some time to load. To provide a better user experience, I want to display a loading GIF while the plugin is loading. However, once the comments ...

Is it possible to use JQuery ScrollTop while in the midst of an animation?

I am facing an issue with resizing containers and scrolling to elements. I have 2 containers that change width dynamically, each containing clickable elements. When a container is clicked, it animates the resize. However, when a clickable element is clicke ...

What steps can be taken to choose a particular class when multiple classes share the same name?

Imagine having a table layout like this: <table class="first-table" border="1" width="400px"> <tr> <td><?php echo $row_cond['title']; ?></td> <td><a class="more" href="#" onClick="slideup() ...

Vue.js is known to issue a series of alerts, one of which includes: "Invalid prop: type check failed for prop 'items'. Expected Array, received String with value ''.”

I currently have my front-end set up using vue-cli and the backend utilizing express. The processing time for fetching data from the backend is 1.7 seconds, but when I make the request under the mounted() lifecycle hook in Vue, I receive warnings because V ...

Passing parameters in the URL during a login process in Laravel

I am encountering an issue where I need to move the routes out of the auth file and into web.php. I want to pass a parameter through the URL for the login process, but it is giving me an error. Route::get('login/{url}', 'Auth\LoginContr ...

How can you customize the bottom and label color for Material-UI TextField when in error or when in focus?

I need to customize the color of my Material UI TextField component for the following states: error focused Currently, I am using version 3.8.1 of @material-ui/core and working with the <TextField /> component. I would like to achieve this withou ...

Creating dynamic class fields when ngOnInit() is called in Angular

I am trying to dynamically create variables in a class to store values and use them in ngModel and other places. I understand that I can assign values to variables in the ngOnInit() function like this: export class Component implements OnInit{ name: st ...

Experience the advanced NgPrime p-dropdown template with templating, filtering, and a clear icon that collapses when wrapped within a form element

Check out this link for a demo Whenever I enclose the code below in a </form> element, the drop-down menu collapses. <h5>Advanced with Templating, Filtering and Clear Icon</h5> <p-dropdown [options]="countries" [(ngModel)]=& ...

Developing a Chessboard Using JavaScript

Seeking help with a Javascript chessboard project. I have successfully created the board itself, but facing difficulty assigning appropriate classes (black or white) to each square. Managed to assign classes for the first row, struggling with the remainin ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

What could be hindering my jQuery function from running the Ajax script?

My jQuery function is supposed to retrieve two values, one for school/college and one for state, and send it to the controller class under the URL "Type&State". However, for some reason, the data is not being passed and nothing is shown in the console ...