Refresh a FormField in the Backend Form

I wonder how to update a form field or the entire form to refresh values if you code a custom button in the backend item form.

I added a custom button within the update.htm which calls a handler. This handler updates the value of the record and in the end, this new data should be shown ofc.

However, whatever I try, it didnt work.

The usual data-request-data="redirect:0, refresh:1" didnt work here for some reason.

I found those two solutions:

And changed my handler return to the following:

        $fieldMarkup = $this->formGetWidget()->renderField('playername', ['useContainer' => true]);
        trace_log($fieldMarkup);
        return [
            '#Form-field-Player-playername' => $fieldMarkup
        ];

I can see that the $fieldMarkup has the correct new code for the field, but it doesnt get rendered. I got the ID directly from the html of the form, so this should also be correct.

I also tried to adapt the solution from this thread:

Also without success.

I see the problem discussed quite often, but no research helped here.

Were there any changes in october 3 to this which I overlooked?

Thx for the help :slight_smile:

Hi there is two methods to refresh form in backend:

(I assume that you used FormController and standart data attributes api).

First is you already mentioned is update field:

$this->initForm($yourModel); // Player model I think
$fieldMarkup = $this->formGetWidget()->renderField('playername', ['useContainer' => true]);
trace_log($fieldMarkup);
return [
   '#Form-field-Player-playername' => $fieldMarkup
];

This should update your playername field via ajax.

But when you need refresh the whole page you can use:

return Redirect::refresh();

This will refresh whole page after you trigger the method.

Hope that helps.

1 Like

worked like a charm. Thanks a lot ^^

1 Like