Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lars-Janssen's avatar

Use SparkForm with FormData

Hi,

Currently I'm using SparkForm for posting data to the server. However now I want to send images aswel. I don't want to lose the SparkForm functionalities.

However because I want to send images I have to make a FormData and add an extra header.

So my question is, is it possible to use FormData in combination with SparkForm?

Right now I've got this:

store() {
    let form = new FormData();

    for(let key in this.form.images) {
        form.append('attachment[' + key + ']', this.form.images[key]);
    }
    Spark.post('/adds', this.form).then((response) => {
        alert('success!');
    }).catch((error) => {
        this.catchMessage(error);
    });
}

But when I dd($request->all()); on my server the images array is empty.

How do I get this to working while keeping the functionalities of the SparkForm?

Thanks!

0 likes
1 reply
Lars-Janssen's avatar
Lars-Janssen
OP
Best Answer
Level 33

Never mind.

Possible like this:

this.form.startProcessing();

axios.post('/adds', form).then((response) => {
    this.form.finishProcessing();
    alert('success!');
},  (error) => {
    this.form.setErrors(error.response.data.errors);
}).catch((error) => {
    this.catchMessage(error);
});

Please or to participate in this conversation.