Oracle apex Apex:如何在动态创建的页面项上显示错误?

Oracle apex Apex:如何在动态创建的页面项上显示错误?,oracle-apex,Oracle Apex,我正在运行Apex 19.2 我有一个页面,其中动态创建了一些项目,如下所示: HTML clob; Html := APEX_ITEM.textarea(p_idx=>32, p_value=>'MyValue',p_item_id=>'MyId',p_attributes=>'class="textarea"'); htp.p(HTML); 页面项目已正确生成: <textarea name="f32" rows="4" cols="40" wr

我正在运行Apex 19.2

我有一个页面,其中动态创建了一些项目,如下所示:

HTML clob;    
Html := APEX_ITEM.textarea(p_idx=>32, p_value=>'MyValue',p_item_id=>'MyId',p_attributes=>'class="textarea"');   
htp.p(HTML);
页面项目已正确生成:

<textarea name="f32" rows="4" cols="40" wrap="VIRTUAL" class="textarea" id="MyId"></textarea>
验证后,错误不会显示在创建的项旁边。通知也会出现,但它是空的。但是,如果我试图在设计器中创建的静态项上显示相同的错误。错误显示正确

有人能帮忙吗?
谢谢。

正如您所发现的,APEX\u项不能以您希望的方式处理APEX\u错误。马克在这里的评论表明,APEX_项目可能不会得到进一步发展,因此可能永远不会

最好的选择可能是将验证逻辑移动到存储过程中。通过参数在一次调用中执行所有验证。除了常规参数之外,添加一个参数,指示响应是否应为JSON。如果是这样,只需返回一个带有错误的JSON文档,否则使用apex_error。这将允许您通过Ajax调用验证逻辑,在您喜欢的地方显示错误,但也可以在提交/页面处理上显示错误,因为客户端验证不可信

以下是您可以遵循的一些步骤,以了解其工作原理。。。首先,在模式中编译以下过程:

create or replace procedure validate_thing(
  p_description in  varchar2,
  p_return_json in  boolean,
  p_json_result out json_object_t
)
is

  l_errors_arr    json_array_t := json_array_t();
  l_error_obj     json_object_t := json_object_t();
  l_item_id       varchar2(30);
  l_error_message varchar2(255);
begin

  if length(p_description) > 10
  then
    l_item_id := 'description';
    l_error_message := 'Description should be less than 10 characters.';

    if p_return_json
    then
      l_error_obj := json_object_t();

      l_error_obj.put('pageItem', l_item_id);
      l_error_obj.put('message', l_error_message);

      l_errors_arr.append(l_error_obj);
    else
      -- Server-side code will not worry about displaying the error with the item as 
      -- this is just a backup for the client-side validation
      apex_error.add_error(
        p_message          => l_error_message,
        p_display_location => apex_error.c_inline_in_notification
      );
    end if;
  end if;

  if p_return_json
  then
    p_json_result := json_object_t();

    if l_errors_arr.get_size() > 0
    then
      p_json_result.put('status', 'error');
      p_json_result.put('errors', l_errors_arr);
    else
      p_json_result.put('status', 'success');
    end if;
  end if;

end;
如您所见,该过程具有执行客户端验证JSON或服务器端验证的逻辑。您需要根据表单的需要添加额外的参数和逻辑

在应用程序中创建一个新的空白页,然后转到新页的页面设计器。在“区域”下的“内容体”上单击鼠标右键,然后选择“创建区域”。将区域的类型设置为PL/SQL动态内容,并将以下代码添加到PL/SQL代码属性:

declare

  html clob;

begin

  -- The div and fieldset wrappers are needed so that APEX will generate an error
  -- message template automatically to display the error inline.
  html := '<div><fieldset>';
  html := html || APEX_ITEM.textarea(p_idx=>32, p_value=>'MyValue',p_item_id=>'description',p_attributes=>'class="textarea apex-item-textarea"');
  html := html || '</fieldset></div>';

  htp.p(html);

end;
declare

  -- Only needed to call validate_thing, not used.
  l_result json_object_t;

begin

  validate_thing(
    p_description => apex_application.g_f32(1), -- This is where the item's value will be when submitting normally
    p_return_json => false, -- This tells validate_thing to use apex_error
    p_json_result => l_result
  );

end;
这是将调用validate_thing并将p_return_json设置为true的代码。请注意,描述值是通过apex_application.g_x01传入的。您有g_x01-g_x20以这种方式工作。您可以利用各种选项通过Ajax发送值,这只是一个示例。有关更多信息,请参阅“下一步使用的文档”

返回到“渲染”选项卡,在新区域上单击鼠标右键,然后选择“创建”按钮。设置要提交的按钮名称。右键单击提交按钮并选择创建动态操作。设置要提交的名称。选择默认显示操作,将其操作设置为执行JavaScript代码,然后将以下代码添加到代码字段:

apex.server.process(
  'DO_VALIDATIONS',
  {
    x01: $x('description').value
  },
  {
    success: function(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.page.submit('SUBMIT');
      }
    },
    error: function( jqXHR, textStatus, errorThrown ) {
      console.log(jqXHR, textStatus, errorThrown)
    }
  }
);
该代码将调用validate_thing,p_return_json设置为false。这将在服务器端重新运行验证,以确保在服务器端执行验证。因为它只是客户端调用的备份,所以我不担心在JS将要执行的项目中显示错误

再次右键单击“处理”,然后选择“创建流程”。设置要执行工作的名称,只需输入null;对于PL/SQL代码属性。将成功消息设置为已运行。。在服务器端条件下,将Type设置为PL/SQL Expression,并在PL/SQL Expression字段中输入not apex\u error.have\u errors\u

此流程表示要在验证通过后运行的实际业务逻辑。只有在Ajax和服务器端验证都通过的情况下,您才会在单击submit后看到成功消息

如果希望测试服务器端验证,请在动态操作中,在提交页面的行之前添加这行JavaScript代码:

$x('description').value = '12345678910';
这将更新文本区域的值,使其超过服务器强制执行的限制

apex.server.process(
  'DO_VALIDATIONS',
  {
    x01: $x('description').value
  },
  {
    success: function(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.page.submit('SUBMIT');
      }
    },
    error: function( jqXHR, textStatus, errorThrown ) {
      console.log(jqXHR, textStatus, errorThrown)
    }
  }
);
declare

  -- Only needed to call validate_thing, not used.
  l_result json_object_t;

begin

  validate_thing(
    p_description => apex_application.g_f32(1), -- This is where the item's value will be when submitting normally
    p_return_json => false, -- This tells validate_thing to use apex_error
    p_json_result => l_result
  );

end;
$x('description').value = '12345678910';