Powerbuilder:如何编写字段的验证表达式
我有一个包含多个字段的数据窗口。我想为名为 amount
的字段编写验证表达式。我还有另外两个名为 debit
和 credit
的字段。如果借记卡和贷记卡的总和大于金额,那么我想向用户显示一条验证消息。
如何在该数据窗口的列规范中编写所需的验证表达式?
我还想为名为 test
的字段编写验证表达式。有一个名为criteria
的字段。当该字段设置为 1 时,我希望测试字段成为必填字段。我该如何为此编写验证表达式?
I have a datawindow containing multiple fields. I want to write a validation expression for a field named amount
. I have another two fields named debit
and credit
. If the sum of debit and credit is greater than amount, then I want to show a validation message to the user.
How can I write the required validation expression in the Column Specification of that datawindow?
I also want to write validation expression for a field named test
. There is a field named criteria
. When this field is set to 1, I want the test field to be a required field. How can I write validation expression for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有不同的方法可以执行此操作,具体取决于您是否必须使用列所需的验证表达式或拥有以不同方式执行此操作的许可证。
设计注意事项
这将是一个自由式数据窗口,您只需要在屏幕上验证一次规则,还是一个表格样式,用户可以批量输入数据集并且需要应用规则每行?
您是否希望要求用户在每次必填字段获得焦点时输入正确的值,或者您想让他们自由导航屏幕并在保存时进行验证?
我希望您会在这里得到一些不同的响应,但我更喜欢仅使用列规范表达式进行简单检查,因为:
但是,我承认 dw 表达式通常非常快。
使用计算字段进行验证规则
一种替代技术的执行速度也很快,并且可以在大多数版本的 PB 中使用。
cf_amount_rule
以及表达式中类似的内容:if (debit + Credit = amount, 0, 1)
cf_amount_rule
,则OO 纯粹主义者可能会认为将逻辑放入数据窗口中是错误的,如果您发现自己在多个数据窗口中放入相同的规则,那肯定是代码味道。但是对于不太可能改变的简单规则,我一次又一次地发现数据窗口在运行这些规则方面非常有效,并且使您不必在其他地方编写大量代码。
可选的好东西
cf_amount_rule
的表达式。There are different ways to do this, depending on whether you have to use the column's required validation expression or have the license to do it different ways.
Design Considerations
Is this going to be a freestyle datawindow where you only need to validate the rule once for the screen, or a tabular style where users can mass enter sets of data and the rule needs to be applied to each row?
Do you want to require the users to enter the correct values each time a required field gets focus or do you want to let them freely navigate the screen and validate at save time?
You'll get some different responses here, I expect, but I prefer to use the column specification expressions only for simple checks because:
However, I'll grant that dw expressions are generally really fast.
Using a Computed Field for Validation Rules
One alternative technique is also a fast performer and can be used in most versions of PB.
cf_amount_rule
and something like this in the expression:if (debit + credit = amount, 0, 1)
cf_amount_rule
and you now have a handy reference point that quickly tells you when there is an errorOO purists might suggest that it's wrong to put logic in the datawindow and if you find yourself putting in the same rule in multiple datawindows that's certainly a code smell. But for simple rules that are unlikely to change I have found time and again that the datawindow is very efficient at running these rules and saving you from having to write lots of code elsewhere.
Optional Goodies
cf_amount_rule
.