【问题标题】:Define default values for fields added with relationExtendManageWidget()为使用 relationExtendManageWidget() 添加的字段定义默认值
【发布时间】:2018-07-17 16:11:55
【问题描述】:

我需要向关系管理器小部件动态添加字段。希望October CMS allow that 使用relationExtendManageWidget() 方法。

所以,这是我的代码:

public function relationExtendManageWidget($widget, $field, $model) {
    $widget->tabs['fields']['_property'] = array(
        'label' => 'Property',
        'span' => 'auto',
        'type' => 'text',
        'tab' => 'Properties',
        'default' => 'test',
    );
}

我的问题是此处定义的默认值“test”仅在我创建新关系时出现。如果我更新现有的,则该值为空。我想知道如何为创建和更新表单的动态字段定义值。

感谢您的帮助

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    这是因为在当前记录中,_property 的 [来自 db] 的值将是 empty,因此由表单 _proprty 创建的字段将获得值 blank,因此它将显示 bloank

    但是对于新记录它的新记录,所以没有数据库记录来填充它的值,它得到默认值test

    在我看来这很好必须是当前的行为

    但如果你想更改behavior,我们可以这样做

    public function relationExtendManageWidget($widget, $field, $model) {
    
        // you can use new array syntex [] in php 7.0
        $fieldConfiguration = [
            'label' => 'Property',
            'span' => 'auto',
            'type' => 'text',
            'tab' => 'Properties',
        ];
    
        // we check record is new or updating
        if($model->exists && $model->_property == '') {
            // we are updating record and seems _property field is ''/empty
            // so we fill it with 'test'
            $model->_property = 'test';
        }
        else {
            // record is new
            $fieldConfiguration['default'] = 'test';
        }
    
        $widget->tabs['fields']['_property'] = $fieldConfiguration;
    }
    

    但现在请确保您永远无法将 empty value 保存为 _property 字段,因为我们决定在其为空时将其设为 test。 [现有记录]

    如果有任何疑问,请发表评论,如果您发布模型,我不确定您是否没有发布模型的架构,因此很难投出正确的答案。

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多