验证复杂 Flex 表单的最佳方法是什么?
我有一个 Flex 应用程序,可以编辑中等复杂的对象,并且我想在编辑器的多个部分上进行一些客户端验证。编辑器是一组嵌套对象:
Form ->
TabNavigator ->
Tab1 ->
Component1.1
Component1.2
Tab2 ->
Component2.1
Component2.2
&c
SubmitButton
结构与数据模型相当接近,UI 是项目所需的,所以如果不需要的话,我不会太快地重构那里的类 - - 时间也是一个问题。我想要做的是为每个 Component* 实例提供验证器:
- 在相关 UI 元素上显示验证工具提示和 UI 提示
- 当表单的任何部分无效时禁用 SubmitButton
- ( (可选)提供足够的信息,以便我可以在表单中显示错误消息。
解决这个问题的最佳方法是什么?
I have a Flex application that edits a moderately complex object, and I'd like to put some client-side validation in place on multiple parts of the editor. The editor is a set of nested objects:
Form ->
TabNavigator ->
Tab1 ->
Component1.1
Component1.2
Tab2 ->
Component2.1
Component2.2
&c
SubmitButton
The structure echoes the data model fairly closely, and the UI is what the project requires, so I'm not going to be too quick to restructure the classes there if I don't have to -- time is an issue too. What I want to do is have validators for each of the Component*
instances that:
- Show the validation tool tips and UI hints on the relevant UI element
- Disable the SubmitButton when any part of the form is not valid
- (Optionally) provide enough information that I can display an error message in the form.
What's the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建表单后,创建一个数组来保存对表单中组件的所有验证器的引用。然后,当用户提交表单时,检查所有输入是否符合您的标准。
When the form is created, create an array to hold references to all the validators of the components in your form. Then when user submits the form, check that all the inputs pass your criterion.
在处理这种用例太多次之后,我最终决定编写自己的 Form 子类,它提供对所有元素的验证作为内置行为。
这个想法从那时起已经发展了很长一段路,但如果您好奇,请查看我在 bitbucket 上发布的开源项目
http://bitbucket.org/rpathsync/smartform
SmartForms(和SmartFormItems)使用XML“描述符”处理表单创建和验证等。这样做的主要原因是允许服务器生成的表单,因为我们经常处理客户端没有先验知识的对象(我们的服务器是可插入的,因此需要服务器提供表单描述符)
那里的代码比您多得多可能想要,但是该存储库中与验证相关的类可能会让您感兴趣。
After dealing with this kind of use case far too many times, I finally decided to write my own subclass of Form that offers validation of all elements as a built-in behavior.
The idea evolved a long way from there, but if you're curious, check out the open source project I published on bitbucket at
http://bitbucket.org/rpathsync/smartform
SmartForms (and SmartFormItems) handle form creation and validation, etc. using an XML "Descriptor". The main reason for this was to allow for server-generated forms since we often deal with objects that the client has no a-priori knowledge of (our server is pluggable hence the need for server provided form descriptors)
There's way more code there than you probably want, but the validation related classes in that repository might be interesting to you.
我想您已经检查了 mx.validators.Validator类及其各种子类,例如
CreditCardValidator
CurrencyValidator
DateValidator
EmailValidator
NumberValidator
PhoneNumberValidator
RegExpValidator
SocialSecurityValidator
StringValidator
ZipCodeValidator
I presume you have checked mx.validators.Validator class and it's various subclasses like
CreditCardValidator
CurrencyValidator
DateValidator
EmailValidator
NumberValidator
PhoneNumberValidator
RegExpValidator
SocialSecurityValidator
StringValidator
ZipCodeValidator