Powerbuilder:如何编写字段的验证表达式

发布于 2024-09-09 08:53:18 字数 297 浏览 0 评论 0原文

我有一个包含多个字段的数据窗口。我想为名为 amount 的字段编写验证表达式。我还有另外两个名为 debitcredit 的字段。如果借记卡和贷记卡的总和大于金额,那么我想向用户显示一条验证消息。

如何在该数据窗口的列规范中编写所需的验证表达式?

我还想为名为 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 技术交流群。

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

发布评论

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

评论(1

怪我太投入 2024-09-16 08:53:18

有不同的方法可以执行此操作,具体取决于您是否必须使用列所需的验证表达式或拥有以不同方式执行此操作的许可证。

设计注意事项

这将是一个自由式数据窗口,您只需要在屏幕上验证一次规则,还是一个表格样式,用户可以批量输入数据集并且需要应用规则每行?

您是否希望要求用户在每次必填字段获得焦点时输入正确的值,或者您想让他们自由导航屏幕并在保存时进行验证?

我希望您会在这里得到一些不同的响应,但我更喜欢仅使用列规范表达式进行简单检查,因为:

  • 那里的复杂逻辑往往难以阅读和维护
  • 如果您要检查多个错误条件,如果您使用列规范的内置错误消息字段,这可能会导致一些难以处理的消息。

但是,我承认 dw 表达式通常非常快。

使用计算字段进行验证规则

一种替代技术的执行速度也很快,并且可以在大多数版本的 PB 中使用。

  • 在数据窗口上有一个计算字段,该字段具有有意义的名称,例如 cf_amount_rule 以及表达式中类似的内容:if (debit + Credit = amount, 0, 1)
  • 该字段对用户不可见,
  • 如果您想在页脚中添加另一个计算字段来求和 cf_amount_rule,则
  • 现在您有一个方便的参考点,可以快速告诉您何时出现错误在保存时(或 pfc_validation PFC 用户的事件)您可以检查总和 > 0 并发布错误消息

OO 纯粹主义者可能会认为将逻辑放入数据窗口中是错误的,如果您发现自己在多个数据窗口中放入相同的规则,那肯定是代码味道。但是对于不太可能改变的简单规则,我一次又一次地发现数据窗口在运行这些规则方面非常有效,并且使您不必在其他地方编写大量代码。

可选的好东西

  • 您可以使用 find() 功能来识别出现错误的特定行,从而使错误消息更加稳健
  • 您可以通过属性更改行或字段的背景颜色,为用户提供有用的视觉提示引用 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:

  • Complicated logic in there tends to get hard to read and maintain
  • If you have multiple error conditions you're checking for, that can lead to some unwieldy messages if you use the column specification's built-in error message field

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.

  • Have a computed field on the datawindow that has a meaningful name such as cf_amount_rule and something like this in the expression: if (debit + credit = amount, 0, 1)
  • Make that field invisible to the user if you want
  • Add another computed field in the footer to sum cf_amount_rule and you now have a handy reference point that quickly tells you when there is an error
  • At save time (or the pfc_validation event for PFC users) you can check for sum > 0 and post an error message

OO 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

  • You can make the error message more robust by using the find() functionality to identify specific rows with the error
  • You provide useful visual cues to your users by changing the row or a field's background color via property expressions that reference cf_amount_rule.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文