【发布时间】:2011-02-03 08:48:20
【问题描述】:
我正在寻找有关创建动态表单的设计理念。
我目前拥有的是一堆具有一些属性/变量的模型,例如:
class Group extends IDKModel {
/**
* @Form
*/
public $title;
/**
* @Form(validation="{my validation rules here}")
*/
public $permissions;
}
在该示例中,Form PHPAnnotation 将其定义为表单元素,并且验证数组将在其中内置验证规则。现在我遇到的问题是我不知道如何实现条件。
例如,如何仅当用户是管理员时才显示 $permissions。如果一天中的时间超过格林威治标准时间 12:00,如何显示 $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")
谁有更简单的想法?
【问题讨论】:
-
一方面,尽量不要将表单呈现逻辑与验证逻辑混为一谈。这不是一个简单的问题,但我确实有一个解决方案。它只是相当大,我不知道在哪里发布。
-
为什么表单展示逻辑要和验证逻辑分开?我正在考虑使用 3 种不同类型的 PHPAnnotations:@Form、@Validation、@Filter,其中 @Form 将其定义为表单元素,@Validation 具有验证它的所有规则,@Filter 将处理表示逻辑。跨度>
-
因为在我所学的设计模式中你不是这样做的。我认为你不应该一起使用注释。
-
什么设计模式?为什么不应该使用注解?
标签: php design-patterns metaprogramming