This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

lucas.sanner54070
lucas.sanner54070

I've struggled for month with this one but I still haven't resolved it.
For instance in my editing form some fields have to be hidden according to the setting of other fields.
But as Ajax is used, the fields supposed to be hidden are still visible after saving. I have to refresh manually (F5 key) for them to be hidden.
I've tried all the bindEvent relating to refresh in the Backend/Behaviors/FormController class (form.beforeRefresh, form.refreshFields,form.refresh) but none of them has worked so far.
It looks like they are not triggered at all after saving.
Can someone show me the way to have my form fields refreshed ?

mjauvin
lucas.sanner54070
lucas.sanner54070

Thanks. I tried this (as a test) in my Categories controller:

public function update_onSave($recordId = null, $context = null)
{   
    $model = Category::findOrFail($recordId);
    $model->slug = "new value";
    $this->initForm($model);

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

    return [
         '#Form-field-Category-slug' => $fieldMarkup
    ];  
}   

but nothing happens (no saving message or whatever), which makes sens since the parent update_onSave() method is not expected to return an array but to make redirect.
Or may be I missed something ?

mjauvin
mjauvin

Are you sure you have the right ID for the field you want to update?

Also, you might want to call the parent::update_onSave() method before returning the field markup.

lucas.sanner54070
lucas.sanner54070

Are you sure you have the right ID for the field you want to update?
Yes, I've checked several times.

I tried with the parent method:

public function update_onSave($recordId = null, $context = null)
{   
    $model = Category::findOrFail($recordId);
    $model->slug = "new value";
    $this->initForm($model);

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

    parent::update_onSave($recordId, $context);

    return [
         '#Form-field-Category-slug' => $fieldMarkup
    ];  
}

Now the form is saved but the field is still not refreshed.
Any idea ?

Last updated

mjauvin
mjauvin

I don't know, it worked when I used this code before.

lucas.sanner54070
lucas.sanner54070

A workaround is to force to redirect whenever the form is updated in the config_form.yaml file:

update:
    title: Edit Category
    redirect: codalia/songbook/categories/update/:id
    redirectClose: codalia/songbook/categories

then in the update.htm file:

            <button
                type="submit"
                data-request="onSave"
                data-request-data="redirect:1"
                data-hotkey="ctrl+s, cmd+s"
                data-load-indicator="Saving Category..."
                class="btn btn-primary">
                <u>S</u>ave
            </button>

It works, but obviously all the Ajax magic is gone...

chocolata
chocolata

Hi guys, does anyone have an idea of how to implement this in conjunction with the following setup?

I've extended the Rainlab Blog plugin, and in my own Plugin.php file, I extend the Post model to listen if a switch (mail_send == 1) has been enabled, in which case I'll send some e-mails and switch off the switch again (mail_send = 0).

Everything works fine behind the scenes, but as others mention, I also have to press F5 to see the switch with the correct setting. This has the unfortunate side effect that, with no refresh, the switch stays enabled and the form will send the e-mails again, as it is still in the on state after saving...

    PostModel::extend(function($model) {
        $model->bindEvent('model.afterSave', function() use ($model) {
            if($model->mail_send) {
                // Some code for sending mail here
                ...

                // Reset the mail_send switch to 0
                $model->mail_send = 0;
                $model->save();

                // Refresh the current page?
                ...
            }
        });
    });

When I try to use $model->formRenderField('mail_send', ['useContainer'=>false]); in the above code, I get the following error message:

"Call to undefined method October\Rain\Database\QueryBuilder::formRenderField()" on line 2483 of E:\Dropbox\Projects\_xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php

Can anyone please set me on the correct path? Thanks in advance.

mjauvin
mjauvin

In can be done if you modify the _post_toolbar.htm partial to include refresh:1 as below:

<?php 
    $isCreate = $this->formGetContext() == 'create'; 
    $pageUrl = isset($pageUrl) ? $pageUrl : null; 
?> 
<div class="form-buttons loading-indicator-container"> 

    <!-- Save --> 
    <a 
        href="nojavascript...;" 
        class="btn btn-primary oc-icon-check save" 
        data-request="onSave" 
        data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>" 
        data-request-before-update="$el.trigger('unchange.oc.changeMonitor')" 
        <?php if (!$isCreate): ?>data-request-data="refresh:1"<?php endif ?> 
        data-hotkey="ctrl+s, cmd+s"> 
            <?= e(trans('backend::lang.form.save')) ?> 
    </a> 

    <?php if (!$isCreate): ?> 
        <!-- Save and Close --> 
        <a 
            href="nojavascript...;" 
            class="btn btn-primary oc-icon-check save" 
            data-request-before-update="$el.trigger('unchange.oc.changeMonitor')" 
            data-request="onSave" 
            data-load-indicator="<?= e(trans('backend::lang.form.saving')) ?>"> 
                <?= e(trans('backend::lang.form.save_and_close')) ?> 
        </a> 
    <?php endif ?> 

    <!-- Preview --> 
    <a 
        href="<?= URL::to($pageUrl) ?>" 
        target="_blank" 
        class="btn btn-primary oc-icon-crosshairs <?php if (!false): ?>hide<?php endif ?>" 
        data-control="preview-button"> 
            <?= e(trans('backend::lang.form.preview_title')) ?> 
    </a> 

    <?php if (!$isCreate): ?> 
        <!-- Delete --> 
        <button 
            type="button" 
            class="btn btn-default empty oc-icon-trash-o" 
            data-request="onDelete" 
            data-request-confirm="<?= e(trans('rainlab.blog::lang.post.delete_confirm')) ?>" 
            data-control="delete-button"></button> 
    <?php endif ?> 
</div> 
mjauvin
mjauvin

You can do this using the backend.form.extendFieldsBefore event to modify the Post toolbar field partial path to a partial you create under your plugin's partials folder for example:

\Event::listen('backend.form.extendFieldsBefore', function ($widget) {
        if (!($widget->getController() instanceof \RainLab\Blog\Controllers\Posts && $widget->model instanceof \RainLab\Blog\Models\Post)) {
            return;
        }
        $widget->fields['toolbar']['path'] = '$/author/plugin/partials/_post_toolbar.htm';
});

Last updated

mjauvin
mjauvin

@lucas.sanner54070 the same could be used to solve your issue, I believe.

chocolata
chocolata

Hi @mjauvin, thank you very much for your in-depth answer! This works, but strangely intermittently it fails. If I switch on my switch and save, the backend form does indeed refresh to the default first page (not the current tab) and it shows my custom flash message that it has worked, but then if I go to the tab where the switch is, the switch is still enabled and stays enabled, even when refreshing. This makes me think that the redirect happens before all code has been executed. Sometimes, it does save correctly though. Should I use a different event to bind to? Now I'm using afterSave, but maybe something else is more appropriate?

Thanks again.

mjauvin
mjauvin

I would personally use model.beforeSave (thus, no need to save the model a second time), but your problem might be that the event is NOT called if there is no change in the model...

chocolata
chocolata

Hi @mjauvin, using model.beforeSave does seem to get the required results. It seems my problem is fixed. Thank you for taking the time for answering me. It might be a good idea to also put it on https://octobertricks.com as I've seen some other people struggling with this same issue.

mjauvin
mjauvin

yes, please add a trick, it's always a good idea!

1-15 of 15

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.