October CMS resources and help articles

Simple and to the point. Optimized by the community.

Update a single field from a FormController AJAX method

by mjauvin, last modified on October 26th, 2020

When using an AJAX handler in your FormController, you can update a single field in your form using this:

public function onYourAjaxHandler($recordID)
   $model = MyModel::findOrFail($recordID);
   $model->fieldToUpdate = "new value";

   $fieldMarkup = $this->formGetWidget()->renderField('fieldToUpdate', ['useContainer' => true]);

   return [
      '#field-id' => $fieldMarkup

If you need to replace the field container, set useContainer => false. If you need to keep the container, set useContainer => true.

Since v452 of OctoberCMS, it is now possible to use the formRenderField() method directly as the "options" argument has been added to it:

   $this->formRenderField('fieldToUpdate', ['useContainer'=>false]);



Post on November 30th, 2020 7:11 AM

Can you explain how to render that field?

in your code, it seems you have to call an ajaxhandler to render a field. so it is a little confusing.

Post on December 8th, 2020 12:56 PM

You can see that this code renders the field.

$this->formGetWidget()->renderField('fieldToUpdate', ['useContainer' => true]);

This is inside an AJAX handler so that it can update a container. So I think you're looking for something else than this. Imagine you have a form with several fields. And when you run onSave or any button action in the form, you want to update just that field without updating the entire form i.e. without refreshing the page--that when you use this provided code. It is called from the data-request of whatever button or element that you are using to cause this element refresh.

We use cookies to measure the performance of this website. Do you want to accept these cookies?