需要有关创建新的“标准”/“语言”的建议
更新:评论中建议我为此创建一个维基。我已经完成了,您可以在这里找到它(如果您希望密切关注和/或贡献)。
我以前从未从事过这样的事情,所以我完全即兴发挥。
我以前从来没有从事过这样的事情,所以
我想研究一种开放的“标准”或“语言”,或者......好吧,我真的不知道该怎么称呼它......使表单验证更容易。我称之为 VRS(验证规则表),但在这个阶段,一切都是可以协商的。
这个想法是创建一个规则表,类似于定义如何验证表单的 CSS。这将需要
- 语法/规范
- VRS 解析器将 VRS 转换为可用的内容
- VRS 处理器将表单数据与规则进行比较并返回响应。
- 响应格式。
像这样的系统的好处是可以采用
- 与平台/语言无关的方式来定义表单验证。
- 一种跨平台、高度可移植的方式来定义表单验证。
- 易于阅读、易于设置、易于修改。
- 客户端和后端集成。
首先,首先要做的事情。语法/规范应该是什么样的。
我认为在线工作的方式是,可以将 VRS 文件指定为隐藏字段,并且应用程序在处理之前通过 VRS 处理器路由提供的表单数据。
举例来说,您可以验证“名称”字段的内容类型,该
name {
content-type: alpha;
}
内容类型可能是三个值之一:字母、数字或字母数字。
希望这是有道理的。我以前从未做过这样的事情,所以我渴望得到其他人的意见。这是我所得到的
------------------------------------------------------------
content-type: (string) alphanumeric | alpha | numeric
Restricts content to either numeric, text or alphanumeric.
------------------------------------------------------------
is-fatal: BOOL
If the rule fails, is it fatal? This could be really useful
for AJAX responses.
------------------------------------------------------------
allow-null: BOOL
wether a field can be empty or not. Good for required fields
and checkboxes
------------------------------------------------------------
pattern-match: (string) email | url | regex
match the field against a pattern.
------------------------------------------------------------
field-match: (string) field_name
compares a field to another field. eg: password confirmation
------------------------------------------------------------
greater-than: INT | FLOAT
less-than: INT | FLOAT
within-range: INT | FLOAT, INT | FLOAT
Pretty self explanatory. With regard to strings however,
the string length is compared against the params.
------------------------------------------------------------
is-unique: (func) connection(host,user,pass), (func) map(table, field)
Check the value against a field in the database to see if
it's unique.
------------------------------------------------------------
date & time validations
This i'm a bit worried about in terms of terminology. I also
want to include dynamic vars in VRS such as
@now
@today
@thisMonth
@thisYear
------------------------------------------------------------
before: STRING | VAR
after: STRING | VAR
Again, self explanatory. Although I'm unsure what the date/time
format should be. UTC?
------------------------------------------------------------
Elapsed Time:
I'm completely stuck on how to handle conditions like
"years elapsed since date is more than 16"
I don't relish the idea of rules as prolix as
years-elapsed-since-now-are-more-than:18;
years-elapsed-since-now-are-less-than:18;
最后,我正在争论开发人员是否应该能够在 VRS 中指定错误/警告,或者他们应该在处理响应时这样做?
所以,有很多东西需要考虑,我希望它是清楚的。我想我的问题是
- 好主意/坏主意?
- 这是正确的语法吗?
- 有没有更优雅的方式来命名规则。
- 缺少什么。
谢谢
更新:一些人表示这个提议的系统是一个坏主意。如果您认为是这样,请提供一个它不起作用的场景。认为这是一个坏主意是一回事,证明这是一个坏主意又是另一回事,而且我希望尽快看到证据证明这是一个坏主意。如果您确实认为表单验证无法变得更容易或更简单,请解释原因。
此外,我知道表单验证并不是一个新问题。然而,目前还没有可移植、跨平台、跨语言的解决方案来解决表单验证问题,而这正是本提案具体解决的问题。
UPDATE: It was suggested in the comments that I create a wiki for this. I have done, you can find it here (should you wish to keep tabs on it and/or contribute).
I've never worked on anything like this before, so I'm completely winging it.
I've never worked on anything like this before, so please
I'm want to work on an open "standard" or "language", or... well, I don't really know what to call it.... to make form validation easier. I'm calling it VRS (Validation Rule Sheets) but at this stage, everything is negotiable.
The idea is to create a sheet of rules, similar to CSS that define how forms should be validated. This will require
- A Syntax / Specification
- A VRS Parser to convert the VRS into something useable
- A VRS Processor to compare the form data against the rules and return a response.
- A response format.
The benefits of a system like this would be
- A platform/language agnostic way to define form validations.
- A cross platform, highly portable way to define form validations.
- Easy to read, easy to setup, easy to modify.
- Client side and backend integration.
First things first though. What should the syntax / specification look like.
The way I see this working online is that a VRS file could be specified as a hidden field and the application routes the supplied form data through the VRS processor before processing it.
By way of an example, you could validate the content type of the "name" field would look like this
name {
content-type: alpha;
}
content-type could be one of three values: alpha, numeric or alpha-numeric.
Hopefully that makes sense. I've never done anything like this before so I'm eager to get other peoples input. Here's as far as I've gotten
------------------------------------------------------------
content-type: (string) alphanumeric | alpha | numeric
Restricts content to either numeric, text or alphanumeric.
------------------------------------------------------------
is-fatal: BOOL
If the rule fails, is it fatal? This could be really useful
for AJAX responses.
------------------------------------------------------------
allow-null: BOOL
wether a field can be empty or not. Good for required fields
and checkboxes
------------------------------------------------------------
pattern-match: (string) email | url | regex
match the field against a pattern.
------------------------------------------------------------
field-match: (string) field_name
compares a field to another field. eg: password confirmation
------------------------------------------------------------
greater-than: INT | FLOAT
less-than: INT | FLOAT
within-range: INT | FLOAT, INT | FLOAT
Pretty self explanatory. With regard to strings however,
the string length is compared against the params.
------------------------------------------------------------
is-unique: (func) connection(host,user,pass), (func) map(table, field)
Check the value against a field in the database to see if
it's unique.
------------------------------------------------------------
date & time validations
This i'm a bit worried about in terms of terminology. I also
want to include dynamic vars in VRS such as
@now
@today
@thisMonth
@thisYear
------------------------------------------------------------
before: STRING | VAR
after: STRING | VAR
Again, self explanatory. Although I'm unsure what the date/time
format should be. UTC?
------------------------------------------------------------
Elapsed Time:
I'm completely stuck on how to handle conditions like
"years elapsed since date is more than 16"
I don't relish the idea of rules as prolix as
years-elapsed-since-now-are-more-than:18;
years-elapsed-since-now-are-less-than:18;
Finally, I'm debating wether devs should be able to specify the errors/warnings in the VRS or should they do that when handling the response?
So, that's a lot to take in and I hope it's clear. My question(s) I guess are
- Good idea / bad idea?
- Is this the right kind of syntax?
- Are there more elegant ways of naming the rules.
- What's missing.
thanks
UPDATE: A few people have stated that this proposed system is a bad idea. If you think so, please provide a scenario in which it wouldn't work. Thinking it's a bad idea is one thing, proving it's a bad idea is another, and I'd like to see proof that it's a bad idea sooner rather than later. If you really think form validation could not be made easier or less tedious, please explain why.
In addition, I'm aware that form validation is not a new issue. However, there is currently no portable, cross platform, cross language solution to address form validation, which is what this proposal is specifically addressing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也喜欢将错误消息放入 VRS 中的想法。但他们应该具体说明失败的规则。
此外,您可能会考虑不开发一种全新的“语言”,而是使用类似 YAML 的东西 已经存在解析。
我认为这种语言很有用,因为您可以使用相同的 VRS 进行客户端和服务器端验证。
PS:我认为这应该是社区维基。
I like the idea of putting the error messages in the VRS too. But they should specific to the rule that failed.
Also, you might consider not developing an entirely new "language" but use something like YAML for which parses already exist.
I see this language as being useful as you could use the same VRS for both client- and server-side validation.
PS: This should be community wiki methinks.
我认为这是一个好主意,如果您可以自己维护它。
记住,语法是你自己制定的。这取决于你(只要它看起来不错)。
不,不是真的。只要名称很明显(看起来像它们本来的样子),并且不太长或令人困惑,那就可以了。
也许您应该在未指定规则时记下规则的默认值。
I suppose it is a good idea, if you can maintain it yourself.
Remember, your making the syntax. It's up to you (so long as it looks decent).
no, not really. So long as the names are obvious (look like what they are), and aren't too long or confusing, then the're fine.
Perhaps you should note default values for the rules when they aren't specified.
好主意/坏主意?
一般来说,这种事情是一个坏主意。这就是 PHP 的用途。
http://www.phpformclass.com/ 有什么问题
http://www.x-code.com/vdaemon_web_form_validation.php
或其他 PHP 表单管理工具?
这是正确的语法吗?
没有。 PHP 出了什么问题?对于这种事情,它有很好的语法。
有没有更优雅的方式来命名规则。
是的。 PHP 对象类。许多其他项目。您不是第一个验证表单输入的人。
缺少什么。
回答基本问题:PHP 出了什么问题?
已经执行此操作的相关项目列表以及您的项目优于所有其他现有项目的具体原因。
Good idea / bad idea?
Generally, this kind of thing is a bad idea. That's what PHP is for.
What's wrong with http://www.phpformclass.com/
http://www.x-code.com/vdaemon_web_form_validation.php
or other PHP form management tools?
Is this the right kind of syntax?
No. What's wrong with PHP? It has good syntax for this kind of thing.
Are there more elegant ways of naming the rules.
Yes. PHP object classes. Numerous Other projects. You're not the first person to validate form input.
What's missing.
Answering the fundamental question: What's wrong with PHP?
A list of related projects that already do this and specific reasons why your project is better than all the other ones already out there.