动态表单构建/元编程/PHP

发布于 2024-10-15 08:04:11 字数 1290 浏览 0 评论 0原文

我正在寻找有关创建动态表单的设计理念。

我目前拥有的是一堆带有一些属性/变量的模型,例如:

 class Group extends IDKModel {

    /**
     * @Form
     */
    public $title;
    /**
        * @Form(validation="{my validation rules here}")
    */
    public $permissions;
}

在该示例中,表单 PHPAnnotation 将其定义为表单元素,验证数组将在其中内置验证规则。现在我遇到的问题是我不知道如何实现条件。

例如,如何仅在用户是管理员时显示 $permissions。如果一天中的时间超过 12:00 GMT,如何显示 $title。基本上任何类型的条件。

摘自 irc.quakenet.org 上的#php:

[10:50] <ramirez> i would not try to stick all of the meta-info under one attribute
[10:50] <ramirez> it'd be much cleaner to do something like
[10:50] <ramirez> @FormElement
[10:51] <ramirez> @Validator(params)
[10:51] <ramirez> etc
[10:52] <ramirez> anyways, I would probably do something like.. @Filter(name="Group",value="admin,editor")
[10:53] <ramirez> then for each filter you want to implement, you'll create a class like "Model_Filter_Group", which would be used for eg. the above filter
[10:53] <ramirez> that class in this case would simply explode the groups by comma and see if user is in any of those groups
[10:54] <ramirez> you can use that for any kind of filtering, eg: @Filter(name="PastTime", value="12:00")

有人有更简单的想法吗?

I'm looking for design ideas about creating dynamic forms.

What I currently have is a bunch of models with a few properties / variables for example:

 class Group extends IDKModel {

    /**
     * @Form
     */
    public $title;
    /**
        * @Form(validation="{my validation rules here}")
    */
    public $permissions;
}

In that example the Form PHPAnnotation defines it as a form element and the validation array will have built in validation rules in it. Now the problem I'm having is I have no idea how to implement conditionals.

For example, how to show $permissions only if a user is an admin. How to show $title if time of day is past 12:00 GMT. Basically any kind of conditional.

Taken from #php at irc.quakenet.org:

[10:50] <ramirez> i would not try to stick all of the meta-info under one attribute
[10:50] <ramirez> it'd be much cleaner to do something like
[10:50] <ramirez> @FormElement
[10:51] <ramirez> @Validator(params)
[10:51] <ramirez> etc
[10:52] <ramirez> anyways, I would probably do something like.. @Filter(name="Group",value="admin,editor")
[10:53] <ramirez> then for each filter you want to implement, you'll create a class like "Model_Filter_Group", which would be used for eg. the above filter
[10:53] <ramirez> that class in this case would simply explode the groups by comma and see if user is in any of those groups
[10:54] <ramirez> you can use that for any kind of filtering, eg: @Filter(name="PastTime", value="12:00")

Anyone have a simpler idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁与争疯 2024-10-22 08:04:11

我建议你看看 Zend Framework 中的 Zend_Form 组件:
Zend 框架参考:Zend_Form

Zend_Form 非常强大且高度可定制,但可以有点太复杂了,无法看到良好的 OOP 设计提供的所有功能。

即使ZF对你没有用,你也可以在那里找到好的想法。

顺便说一句:手册仅涵盖基础知识以及网上找到的教程

I suggest you take a look to Zend_Form component in Zend Framework:
Zend Framework Reference:Zend_Form

Zend_Form is very powerful and highly customizable but can be a bit too complex to see all of its capabilities provided by good OOP design.

Even if ZF of no use for you, you can find good ideas there.

btw: manual covers only basics as well as tutorials found on the web

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文