Homepage
Main navigation
Main content
Additional information
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Winter CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
When using an AJAX handler in your Controller, you can update a single form field as shown below: ``` public function onYourAjaxHandler($id = null, $context = null) { if (is_null($id)) { $model = $this->formCreateModelObject(); $model = $this->formExtendModel($model) ?: $model; } else { $model = $this->formFindModelObject($id); } $fieldName = 'fieldToUpdate'; $fieldId = sprintf("#Form-field-%s-%s-group", class_basename(get_class($model)), $fieldName); $model->{$fielName} = "new value"; $this->initForm($model, $context); $fieldMarkup = $this->formRenderField($fieldName, ['useContainer'=>false]); return [ $fieldId => $fieldMarkup, ]; } public function create_onYourAjaxHandler() { return $this->onYourAjaxHandler(context:"create"); } public function update_onYourAjaxHandler($id) { return $this->onYourAjaxHandler($id, context:"update"); } ``` You can find the Id of the Form Field Group by inspecting your Form's HTML code using the Debug Tool Inspector in your browser, but the above code should be able to generate the Id based on the value you assign to `$fieldName`. If you need to *replace* the field container, set `useContainer => false`. If you need to *keep* the container, set `useContainer => true`. Tip: if you want to update a repeater field from another custom formwidget on the same form, just use this instead: ``` $controller = $this->controller; $controller->initForm($model); $fieldMarkup = $controller->formRenderField('fieldToUpdate', ['useContainer'=>false]); ``` And return the result shown previously.
References
Add additional online resources to your trick
×
Name
URL
×
Name
URL
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline