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: Note: if using the Javascript API, don't forget to add the `form` attribute to your JSON request. (ref. https://wintercms.com/docs/v1.2/docs/ajax/javascript-api) ``` public function onYourAjaxHandler($id = null, $context = null) { if (is_null($id)) { $model = $this->formCreateModelObject(); $model = $this->formExtendModel($model) ?: $model; } else { $model = $this->formFindModelObject($id); } $this->initForm($model, $context); $formWidget = $this->formGetWidget(); // use method below instead to work with the RelationController's manage form // $formWidget = $this->relationGetManageWidget(); $fieldName = 'fieldToUpdate'; $field = $formWidget->getField($fieldName); $fieldId = sprintf("#%s-group", $field->getId()); $field->value = 'My Dynamic Value'; $fieldMarkup = $formWidget->makePartial('field', ['field' => $field]); return [ $fieldId => $fieldMarkup, ]; } public function create_onYourAjaxHandler() { return $this->onYourAjaxHandler(context:"create"); } public function update_onYourAjaxHandler($id) { return $this->onYourAjaxHandler($id, context:"update"); } ``` 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