表格表单验证无需提交
在 APEX 3.2 中,我希望能够运行 JavaScript 验证来检查输入的数据并在表格形式的每行上方显示适当的消息。
我不确定这将如何工作,因为它是表格形式并且用户将能够添加/删除行。
感谢任何想法或建议。
谢谢。
In APEX 3.2, I want to be able to run JavaScript validations to check the data entered and display the appropriate message above each row in the tabular form.
I'm not sure how this would work given that it is a tabular form and the user will be able to add/delete rows.
Appreciate any ideas or suggestions.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,在表格表单上进行一些 javascript 验证有点复杂,您需要知道自己在做什么。
首先,您需要知道要检查的元素的 ID 或名称。您可能知道,表格形式的元素在提交时存储在 apex 的数组中,并通过
apex_application.g_f01/g_f02/...
访问这反映在 html 代码中,生成的元素也将属性“name”设置为它们所属的列。 id 还保存列以及行索引。但请注意,只有“隐式”创建项目时,才会像这样生成此 id,即您没有使用 apex_item 调用 (
apex_item.textbox(...)
) 编写查询。另一个但是,只有保存状态的字段才会定义数组列。仅显示为“仅显示”的项目不会使用输入标签生成,而只会作为文本保存在 td 标签中。
总而言之,当您知道这一点时,接下来的步骤应该足够简单了。查看页面源代码,并记下您想要定位的元素。例如,我去了工作领域。
在页面的 javascript 部分中,我添加了以下 2 个函数。
validate_job 仅对一个字段(元素 elJob)进行验证。我使用的验证非常基本,由您决定您想要的复杂程度。
如果您想在此处引用同一行中的其他字段,您可以执行以下操作:从 id 中提取 rowindex(如果有)。如果不包含 it,则获取父 TR,然后使用
.children("input[name='f##'")
获取同一行中的输入元素。或者,如果您需要根本不保存状态的项目的值,则需要获取 TR 元素,然后通过 headers 属性(保存列名称)找到包含所需元素的 TD。onload 调用bind_validations。如果允许创建行,请将单击事件绑定到addrow按钮并调用bind_validations。
不过,这只是一个适当的警告。到目前为止,我已经在一些应用程序中使用了 javascript 验证,但我知道它们只会被少数人使用,而且只能在内部使用。这只是一个字段,需要进行一些验证。当验证失败时,我让光标重新聚焦在该字段上,这样他们就无法跳到下一条记录并更改它。要么给出了有效值,要么他们重新加载页面或取消操作。像这样设置,他们也不能按应用更改,因为模糊事件也会触发,从而验证字段。
当你的受众更大时,事情就会变得更加不确定:我的 javascript 被禁用了吗?如果他们找到解决办法怎么办?威兹小子?
我仍然喜欢它提供的即时反馈,但在更关键的环境中,我还会使用服务器端验证。为此,您需要验证“返回错误文本的函数”类型。查看此页面<例如 /a> 或 这个 提供一些有用的提示(至少对于 4.0 之前的版本) !)。另外:apex 4.1 在表格表单验证方面确实改进了很多! ;)
Okay, doing some javascript validations on tabular forms is a bit complex, and you need to know what you're doing.
First off, you will need to know the ids or names of the elements you wish to check. As you may know, elements in tabular forms are stored in arrays in apex on submit, and are accessed through
apex_application.g_f01/g_f02/...
This is reflected in the html code, and the generated elements also have the attribute 'name' set to the column they belong to. The id also holds the column, plus the rowindex. Warning though, this id is only generated like this when the item is created 'implicitly', ie you did not write your query with apex_item calls (
apex_item.textbox(...)
).Another but is that only fields of which the state is saved will have an array column defined. An item which you'd only show as 'display only', will not be generated with an input tag, and will just be held as text in a td tag.
All by all, when you know that, the next steps should be straightforward enough. Take a look at the page source, and take a note of the elements you wish to target. For example, i went for the job field.
In the javascript section of the page i then added the following 2 functions.
validate_job does the validation of just one field, the element elJob. The validation i used is just very basic, it's up to you to determine just how complex you want it.
If you want to reference other fields in the same row here, you can do several things: extract the rowindex from the id, if you have it. If it doesn't hold the it, get the parent TR, and then use
.children("input[name='f##'")
to get an input element in the same row. Or if you need the value of an item which does not save state at all, you'll need to get the TR element, and then find the TD which contains the element you need through the headers attribute, which holds the column name.Call bind_validations onload. If you allow rows to be created, bind a click event to the addrow button and call bind_validations.
Just a proper warning though. I've used javascript validations in some apps so far, but i knew they were only going to be used by a small number of people, and then only internally. It was only one field, with some validations. I made the cursor refocus on the field when the validation failed, so they couldn't jump to the next record and change that aswell. Either a valid value was given, or they reloaded the page or canceled the action. Set up like this, they can't press apply changes either, as the blur event would also fire, validating the field.
When your audience is larger, it gets a bit more iffy: what i javascript is disabled? What if they find some way around? Wizzkids?
I still like the immediate feedback it gives, but in a more critical environment i'd also use the server-side validations. To do this, you need a validation of the type "function returning error text". Check out this page for an example, or this one for some usefull tips (at least for pre 4.0!). Also: apex 4.1 really improves a lot on tabular form validations! ;)