This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
How can I get the attributes (set in fields.yaml) of a given field from my controller in backend ?
It seems that there is no specific method in the Controller or in the Model object.
The solution would be something like $field = $form->getField('field_name');
but how can I get the form object from the controller ?
Last updated
From the controller implementing the FormController
behavior, do this:
$form = $this->formGetWidget();
$field = $form->getField('fieldName');
Depending on where exactly you try to do this, the form might not be initialized yet... so you may have to do this manually like so:
// $recordId contains the model id for the model being edited, or null if it is a new model being created.
$model = $recordId ? $this->formFindModelObject($recordId) : $this->formCreateModelObject();
$this->initForm($model);
$form = $this->formGetWidget();
$field = $form->getField('fieldName');
The first version works fine for me as I use it in the update_onSave Ajax handler.
Thanks for your help.
Last updated
How exactly do you use the update_onSave() in your controller? Care to share the resulting code?
mjauvin said:
How exactly do you use the update_onSave() in your controller? Care to share the resulting code?
Sure.
public function update_onSave($recordId = null, $context = null)
{
$this->asExtension('FormController')->update_onSave($recordId, $context);
$myModel = MyModel::find($recordId);
if ($redirect = $this->makeRedirect('update', $myModel)) {
return $redirect;
}
$form = $this->formGetWidget();
// Prepares the value of the fields to be refreshed....
...
return $updatedValues;
}
The FormController's update_onSave() already returns a redirect, why don't you use this?
public function update_onSave($recordId = null, $context = null)
{
$this->context = strlen($context) ? $context : $this->getConfig('update[context]', self::CONTEXT_UPDATE);
$model = $this->controller->formFindModelObject($recordId);
$this->initForm($model);
$this->controller->formBeforeSave($model);
$this->controller->formBeforeUpdate($model);
$modelsToSave = $this->prepareModelsToSave($model, $this->formWidget->getSaveData());
Db::transaction(function () use ($modelsToSave) {
foreach ($modelsToSave as $modelToSave) {
$modelToSave->save(null, $this->formWidget->getSessionKey());
}
});
$this->controller->formAfterSave($model);
$this->controller->formAfterUpdate($model);
Flash::success($this->getLang("{$this->context}[flashSave]", 'backend::lang.form.update_success'));
if ($redirect = $this->makeRedirect($this->context, $model)) {
return $redirect;
}
}
You could just do:
if ($redirect = $this->asExtension('FormController')->update_onSave($recordId, $context)) {
return $redirect;
}
$form = $this->formGetWidget();
...
Also, I'm curious as to what you're doing in your "Prepares the value of the fields to be refreshed...."
mjauvin said: You could just do:
if ($redirect = $this->asExtension('FormController')->update_onSave($recordId, $context)) { return $redirect; } $form = $this->formGetWidget(); ...
I didn't realized I could do it that way. Thanks.
mjauvin said: Also, I'm curious as to what you're doing in your "Prepares the value of the fields to be refreshed...."
Well, I’ve been looking for a while a solution to refresh some fields after saving a form with Ajax. So this is what I found:
controllers/mymodel/update.htm
<button
type="button"
data-request="onSave"
data-request-data="redirect:0"
data-request-success="$.fn.refreshFields(data);"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="Saving..."
class="btn btn-primary">
Save
</button>
assets/js/mymodel.js
(function($) {
...
$.fn.refreshFields = function(data) {
$('#Form-field-Mymodel-field_name').val(data.field_name);
}
...
})(jQuery);
and finally in the controller:
public function update_onSave($recordId = null, $context = null)
{
if ($redirect = $this->asExtension('FormController')->update_onSave($recordId, $context)) {
return $redirect;
}
$myModel = MyModel::find($recordId);
$form = $this->formGetWidget();
// Prepares the value of the fields to be refreshed....
...
return $updatedValues;
}
The "refreshing part" is done through the js function. It seems to work pretty well so far...
1-8 of 8