首页 > 解决方案 > Octobercms Builder - 如何为后端表单编写验证代码和修改字段

问题描述

在使用了 10 年的 Joomla 之后,我对这个美妙的 10 月很陌生,并希望继续努力。我正在使用出色的 Builder 插件,并希望在后端使用“创建”或“更新”表单进行“复杂”验证。

经过多次浏览网络,我不知道如何以及在哪里放置代码以添加业务验证规则?

我使用 yaml 选项来获得动态字段和简单的验证。现在我想要复杂的规则,比如:如果一个类别是 X,一个类型是 Y,那么(文本)名称字段是 SET 到 'ZZZ' 有人可以让我在 PHP 中添加这样的代码吗?

我能够读取字段值,但无法以编程方式更改输入。经过多次尝试,我被困在这个问题上。任何帮助将不胜感激。谢谢。

[编辑] 在 Raja 帮助之后,我修改了我的代码:PHP 模型

class Event extends Model {
    use \October\Rain\Database\Traits\Validation;
    use \October\Rain\Database\Traits\SoftDelete;

    public $rules = [];

    // in models/your_object_model
    public function filterFields($fields, $context = null) {
        echo "\r\n".'filterFields() : '.$this->nom."\r\n";
        // var_dump($fields->nom);
        if ( $this->nom == 'toto' ) {
            echo $this->nom.' is french'."\r\n";
            $fields->pays->value = 'FR';
            $fields->pays->hidden = true;
            $fields->ville->value = 'Paris';
        }
    }
}

亚AML:

fields:
    nom:
        label: Nom
        span: full
        type: text
    description:
        label: Description
        size: ''
        span: full
        type: richeditor
    pays:
        label: Pays
        span: auto
        default: US
        type: text
        dependsOn: nom
    ville:
        label: Ville
        span: auto
        default: NY
        dependsOn: nom
        type: text

我的理解是 filterFields() 是基于 YAML dependsOn: nom 触发的。

当我填写“Nom”并单击“描述”时,结果现在是:

1)我仍然无法在函数中更改表单中的值,因为我显示字符串“toto is french”

2)在我尝试修改的文本字段上,我得到了无尽的微调器,现在无法用鼠标输入字段,只能用键盘输入

3)如何删除绿色字符串 X_OCTOBER_ASSETS ?

描述 clic 上的结果图片

标签: octobercmsoctobercms-backend

解决方案


要操作表单字段,您应该覆盖filterFields模型文档中的方法。

假设我们的模型有 4 个字段,其中NamePublish取决于 和 的CategoryType

字段定义

category:
    label: Category
    type: dropdown
    options:
        category_a: Category A
        category_b: Category B
        category_c: Category C
type:
    label: Type
    type: dropdown
    options:
        type_a: Type A
        type_b: Type B
        type_c: Type C
name:
    label: Name
    comment: 'Comment..'
    dependsOn: [category,type] 

publish:
    label: Publish
    type: switch
    comment: 'Comment..'
    default: true
    dependsOn: [category,type]

注意:使用选项设置字段依赖dependsOn项。这里的Name领域取决于categorytype

模型

public function filterFields($fields, $context = null)
{
    // Category selected has a value of 'category_b';
    if ($this->category == 'category_b') {

       // Try this to see available properties on this field type
       // var_dump($fields->name);

        // Category = 'category_b' && Type selected = 'type_c'...
        if ($this->type == 'type_c') {
            $fields->name->value = 'Category set to B and Type to C';
            $fields->publish->value = false;
            $fields->publish->hidden = true;
        } else {
            $fields->name->value = "Category set to B";
            $fields->name->comment = 'Comment changed..';
            $fields->publish->value = false;
            $fields->publish->comment = "Switch has been turned off";
        }
    }
    elseif (($this->category == 'category_a') && ($this->type == 'type_a')) {
        $fields->name->value = 'Category set to A';
        // ect...
    }
}

这是非常直接的。您可以更新某个字段的所有属性,如果您只需要显示评论或隐藏某些内容,这将非常有用。该context参数使您可以更好地控制何时应用过滤器,例如update

希望这可以帮助您入门。

编辑

为什么要在函数中添加echo $this->nom.' is french'."\r\n";和?echo "\r\n".'filterFields() : '.$this->nom."\r\n";filterFields

只需删除这些行,您就不会出现任何错误。

在我的示例中,我提到这是一种可视化数据响应的快速方法。查看Backend\Classes\FormBackend\Classes\FormField以更好地了解它的工作原理。

您的方法可能如下所示:

public function filterFields($fields, $context = null) {
    if ( $this->nom == 'toto' ) {
        $fields->pays->value = 'FR';
        $fields->pays->hidden = true;
        $fields->ville->value = 'Paris';
    }
}

推荐阅读