更新时间:2021-08-01 16:21:14
有多种方法可以解决此问题.您目前正在混合使用动态操作和页面级验证以及流程,这些流程无法很好地发挥作用.我建议您将所有逻辑转移到动态操作"上.以下是逐步说明,以帮助您完成我认为您想做的事情.您可以从中学到东西,然后将想要的东西集成到解决方案中.
There are different ways you could approach this. You're currently using a mix of Dynamic Actions and page-level validations and processes that aren't going to play well together. I suggest you move all the logic to Dynamic Actions. Here are step by step instructions to do what I think you're trying to do. You can learn from this and then integrate what you want back to your solution.
选择默认情况下为动态操作创建的显示操作.将操作设置为执行PL/SQL代码,然后将以下代码复制粘贴到 PL/SQL Code 属性中.
Select the Show action that was created by default for the Dynamic Action. Set Action to Execute PL/SQL Code and copy-paste the following code into the PL/SQL Code attribute.
declare
l_result_obj json_object_t := json_object_t();
l_errors_arr json_array_t := json_array_t();
l_error_obj json_object_t;
begin
if :P17_FIRST_NAME is null
then
l_error_obj := json_object_t();
l_error_obj.put('pageItem', 'P17_FIRST_NAME');
l_error_obj.put('message', 'First Name is required.');
l_errors_arr.append(l_error_obj);
end if;
if :P17_LAST_NAME is null
then
l_error_obj := json_object_t();
l_error_obj.put('pageItem', 'P17_LAST_NAME');
l_error_obj.put('message', 'Last Name is required.');
l_errors_arr.append(l_error_obj);
end if;
if l_errors_arr.get_size() > 0
then
l_result_obj.put('status', 'error');
l_result_obj.put('errors', l_errors_arr);
:P17_RESULT := l_result_obj.to_string();
return;
end if;
null; -- do "success" processing here
l_result_obj.put('status', 'success');
l_result_obj.put('message', 'Hi ' || :P17_LAST_NAME || ' ' || :P17_LAST_NAME ||
'! You will now be redirected to Google.');
:P17_RESULT := l_result_obj.to_string();
end;
如您所见,验证现在在流程内完成. PL/SQL代码利用了Oracle 12.2引入的JSON类型.如果您使用的是旧版本的数据库,则可以修改代码以改为使用APEX_JSON.
As you can see, the validations are now being done inside the process. The PL/SQL code is making use of the JSON types introduced with Oracle 12.2. If you're on an older version of the database, you can adapt the code to use APEX_JSON instead.
选择默认情况下为动态操作创建的显示操作.将 Action 设置为 Execute JavaScript Code (执行JavaScript代码),然后将以下代码复制并粘贴到 Code 属性中.
Select the Show action that was created by default for the Dynamic Action. Set Action to Execute JavaScript Code and copy-paste the following code into the Code attribute.
var result = JSON.parse($v('P17_RESULT'));
apex.message.clearErrors();
if (result.status === 'error') {
for (var idx = 0; idx < result.errors.length; idx++) {
result.errors[idx].type = 'error';
result.errors[idx].location = ['page', 'inline'];
result.errors[idx].unsafe = false;
}
apex.message.showErrors(result.errors);
} else if (result.status === 'success') {
apex.message.alert(result.message, function(){
apex.navigation.redirect('https://google.com');
});
}
JavaScript代码从过程中获取结果,并显示错误消息或警报.我使用的是apex.message.alert
而不是apex.message.showPageSuccess
,因为前者在关闭消息时支持回调.取消该消息后,apex.navigation.redirect
会将用户带到Google.
The JavaScript code takes the result from the process and either displays error messages or an alert. I'm using apex.message.alert
instead of apex.message.showPageSuccess
because the former supports a callback when the message is dismissed. When the message is dismissed, apex.navigation.redirect
takes the user to Google.
这是最后的样子:
我希望这里有足够的信息供您了解正在发生的事情.如果您有任何疑问,请告诉我.您可以在以下位置找到apex.navigation
和apex.message
的文档: https://apex.oracle.com/jsapi
I hope there's enough information here for you to understand what's going on. Let me know if you have any questions. You'll find the documentation for apex.navigation
and apex.message
here: https://apex.oracle.com/jsapi
P.S.这是一个使用APEX_JSON的PL/SQL代码的示例.
P.S. Here's an example of what the PL/SQL code would look like using APEX_JSON.
declare
l_error_count pls_integer := 0;
l_result_obj clob;
begin
apex_json.initialize_clob_output;
apex_json.open_object();
apex_json.open_array('errors');
if :P17_FIRST_NAME is null
then
l_error_count := l_error_count + 1;
apex_json.open_object();
apex_json.write('pageItem', 'P17_FIRST_NAME');
apex_json.write('message', 'First Name is required.');
apex_json.close_object();
end if;
if :P17_LAST_NAME is null
then
l_error_count := l_error_count + 1;
apex_json.open_object();
apex_json.write('pageItem', 'P17_LAST_NAME');
apex_json.write('message', 'Last Name is required.');
apex_json.close_object();
end if;
apex_json.close_array();
if l_error_count > 0
then
apex_json.write('status', 'error');
apex_json.close_object();
:P17_RESULT := apex_json.get_clob_output();
apex_json.free_output;
return;
end if;
null; -- do "success" processing here
apex_json.write('status', 'success');
apex_json.write('message', 'Hi ' || :P17_LAST_NAME || ' ' || :P17_LAST_NAME ||
'! You will now be redirected to Google.');
apex_json.close_object();
:P17_RESULT := apex_json.get_clob_output();
apex_json.free_output;
end;