How to update an editor field to reflect an uploaded file name

How to update an editor field to reflect an uploaded file name

kanweikanwei Posts: 14Questions: 4Answers: 0

I have an editor field containing plain text, like this:

                    label: "Form URL",
                    name: "url_forms"

and just below it, a file upload like this:

                    label: "Form upload",
                    name: "form_upload",
                    type: "upload",
                    display: function(file_id) {
                        return '<span>' + file_id + '</span>';
                    },
                    ajax: "/api/admin/foo-form-upload",
                    ajaxData: function(d) {
                        d.append('foo_id', editor.ids());
                    },
                    dragDrop: false,
                    noImageText: "No image"

Most of the time, I will only populate the 'url_forms' field with a url from the backend. However, when the user uploads a new form, we want to save it to a bucket on the backend and update that field with the new filename.

I am using a dependent field like this:

        editor.dependent('form_upload', function(val, data, callback, e) {
            console.log('val', val);
            console.log('data', data);
            editor.field('url_forms').set(val);
            callback(true);
        });

So that when the file is uploaded via the 'form_upload' field, then 'val' should have either the new filename or some kind of object of file info containing the filename that I can then use to update the 'url_forms' field.

However, when I look at the value of 'val' logged in the console, I see this:

val <empty string>

So 'val' in the callback from 'form_upload' is empty, even though the file upload has succeeded.

What am I doing wrong? How can I access the file url returned from the upload endpoint and display it in the 'url_forms' field?

Thanks!

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Hi,

    postUpload is probably the event to listen for here rather than using the dependent() method. I don't think we've really tested that against an upload field. I've made a note to do so!

    Allan

Sign In or Register to comment.