Octobercms 禁用相关字段

Octobercms disble fields in relation

我想在更新上下文中禁用表单域,见图:

我试过了,但没用

public function filterFields($fields, $context = null)
{
    if($context == 'update') {
        $fields->books->disabled = true;
        $fields->user->disabled = true;
    }
}

您似乎正试图让 relation manager field 禁用(只读)

但我确定他们不是关注same pattern as normal widgets do

不能像那样直接禁用它们,因为我找到了另一种简单的方法

您可能使用过 partial for rendering this relational field ( book | user ) 并且您的部分 _books.htm 看起来像这样。

<?= $this->relationRender('comments', ['readOnly' => false]) ?>

You need to change it with this one

<?php if($this->widget->form->context == 'update'): ?>
    <?= $this->relationRender('comments', ['readOnly' => true]) ?>
<?php else: ?>
    <?= $this->relationRender('comments', ['readOnly' => false]) ?>
<?php endif; ?>

magic config 值 是这个 readOnly 属性 它将生成列表 read-only or active working.

尝试一下它会起作用,如果不行请评论。