milkyway-multimedia / ss-zen-forms
允许您在显示之前装饰 Silverstripe 表单,添加额外功能
Requires
- sheadawson/silverstripe-zenvalidator: dev-master
- silverstripe/framework: ~3.1
Suggests
- milkyway-multimedia/ss-mwm-formfields: Some extra form fields that you may handy and complement this module
This package is not auto-updated.
Last update: 2024-09-14 17:48:20 UTC
README
仍在积极开发中
由于一些限制,这可能不会按照我的预期工作,因此 API 将会频繁更改,直到我满意为止。如果您必须使用它,请谨慎使用。
Zen Forms 是一个模块,允许使用装饰器模式对 Silverstripe 表单和验证器进行更改。名称来自一些其他出色的 Silverstripe 开发者 @sheadawson 和 @unclecheese
装饰器模式?
装饰器模式通过将新方法包装到其他对象中工作,只需将它们作为构造函数传递即可。您可以在同一对象周围包装任意数量的装饰器。它还非常适合将一些逻辑从模型中分离出来,这样模型类就不会变得杂乱。
在 Silverstripe 框架中,SS_ListDecorator 的工作方式就是一个这样的例子。
您可以在不仅仅是表单中使用此模式。您可以将装饰器包装在需要临时功能的数据对象周围,这些功能可以轻松注入/更改,而无需更改底层模型类。
为什么不直接使用扩展呢?
扩展很棒,但有时并非每个对象都需要该扩展(例如,我不需要 CMS 中的表单使用 Twitter Bootstrap 模板和功能)。
装饰器在 每个实例 上而不是在 每个类 上工作,有时这正是您真正需要的。
如果您更喜欢扩展版本,unclecheese 提供了 bootstrap-form 扩展 by @unclecheese。
为什么使用 Zen 验证器?
默认的 Silverstripe 验证器目前不支持 JS 验证,所以我使用了 @sheadawson 的 Zen 验证器。我已经包含了一些与 FormFieldBootstrapper 装饰器配合得很好的约束。
安装
将以下内容添加到您的 composer.json 文件中
"require" : {
"milkyway-multimedia/silverstripe-zen-forms": "dev-master"
}
用法
使用起来相当简单,也很容易自己制作。例如,如果您想创建一个符合 Twitter Bootstrap 布局方案的表单,您在定义表单时可以这样操作
// $form now has FormBootstrapper methods attached, as well as the underlying form methods
$form = new FormBootstrapper(new Form($controller, $name, $fields, $actions));
您也可以包裹任意多个
$form = new ModaliseForm(new FormBootstrapper(new Form($controller, $name, $fields, $actions)));
以下是模块附带的一些装饰器。每个装饰器在 docs/ 文件夹中都有更多文档
对于表单
- FormBoostrapper:将表单设置为使用 Twitter Bootstrap 模板方案。此方案包含来自 UncleCheese 模块的大量代码。如果您使用此方案,字段列表也将自动装饰。
- ModaliseForm:将表单设置为在模态框中显示。
对于字段列表
- FieldListBootstrapper:将字段列表设置为使用 Twitter Bootstrap 模板方案。此方案包含来自 UncleCheese 模块的大量代码。如果您使用此方案,字段也将自动装饰。
对于字段
- FormFieldBoostrapper:将字段设置为使用 Twitter Bootstrap 模板方案。此方案包含来自 UncleCheese 模块的大量代码。
- CompositeFormFieldBootstrapper:如果您使用的是具有与其中某个子字段相关联的操作的复合字段,则需要使用此装饰器。在必要时,它将自动与 FormBootstrapper 和 FieldListBootstrapper 一起使用。
要自己制作,您可以简单地扩展特定的装饰器、BaseDecorator 或实现 \Milkyway\ZenForms\Contracts\Decorator。
class TreatDataObjectSpecial extends BaseDecorator {
// Do something special with the decorator, and you can refer to original object using $this->original();
}
class TreatFormFieldSpecial extends FormFieldDecorator {
// Do something special with the decorator, and you can refer to original object using $this->original();
}
约束
此模块自动引入了Silverstripe ZenValidator模块,因为它增加了新的约束。
- Milkyway\SS\ZenForms\Constraints\RequiredIf
- Milkyway\SS\ZenForms\Constraints\ValidPassword
- Milkyway\SS\ZenForms\Constraints\ConfirmPassword
- Milkyway\SS\ZenForms\Constraints\Multiple
ConfirmedPasswordField
当您将ConfirmedPasswordField包装起来时,它允许您使用密码测量辅助工具和密码生成器。但它仅在FormFieldBootstrapper包装下才能正常工作,并且您必须在脚本中包含jquery.complexify.js才能使其工作(它不包括在模块中)。
许可证
- MIT
版本
- 版本 0.2(Alpha)
联系信息
Milkyway Multimedia
- 主页: http://milkywaymultimedia.com.au
- 电子邮件: mell@milkywaymultimedia.com.au
- Twitter: @mwmdesign