octobercms - 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 ?
解决方案
要操作表单字段,您应该覆盖filterFields
模型文档中的方法。
假设我们的模型有 4 个字段,其中Name
和Publish
取决于 和 的Category
值Type
。
字段定义
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
领域取决于category
和type
。
模型
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\Form和Backend\Classes\FormField以更好地了解它的工作原理。
您的方法可能如下所示:
public function filterFields($fields, $context = null) {
if ( $this->nom == 'toto' ) {
$fields->pays->value = 'FR';
$fields->pays->hidden = true;
$fields->ville->value = 'Paris';
}
}
推荐阅读
- apache-kafka-connect - kafka-connect sink 连接器 pk.mode 用于具有自动增量的表
- javascript - React Native Firebase 拉取和推送数据问题
- microsoft-graph-api - 我可以使用 Microsoft Bot Framework 实现 Maker/Checker 机器人吗?
- google-chrome - Google Chrome 版本 91 导致带有 Struts 的旧版 JSP 丢失数据和格式
- r - 对矩阵列名进行排序以匹配列表中的元素顺序
- excel - 如何通过工作表名称在 Perl 中读取 Excel 文件
- javascript - 数据表不显示数据(Laravel 8.4.x)
- java - 如何在水平滚动条中查看 ArrayList?
- mule - 骡流终止
- c++ - 为什么我可以使用运行时加载的 dll 未导出的函数