已修正数据标记和 modified 属性

文本表单上的每个项都具有已修正数据标记,此标记是一个状态值,它指示是否认为用户自上次显示表单后更改了表单项。

项的已修正数据标记与项的 modified 属性不同,后者是在程序中设置的并且预设已修正数据标记的值,如后文所述。

与用户进行交互

在大多数情况下,当程序向用户显示表单时,已修正数据标记被预设为 no;然后,如果用户更改表单项中的数据,则已修正数据标记被设置为 yes,并且程序逻辑可以执行下列操作:
  • 使用数据表或函数来验证已修改的数据(当项的已修正数据标记是 yes 时,此操作自动发生)
  • 检测用户是否已修改了项(例如,通过使用 if item modified 类型的条件语句)

用户通过在项中输入字符或通过删除字符来设置已修正数据标记。在提交表单之前,已修正数据标记保持已设置状态,即使用户将字段内容恢复为最初显示的值亦如此。

由于错误而重新显示表单时,表单仍处理同一个 converse 语句。因此,当重新显示表单时,在执行 converse 时被修改的任何字段都将已修正数据标记设置为 yes。例如,如果将数据输入到带有验证器函数的字段中,则该函数可以调用 ConverseLib.validationFailed 函数来设置错误消息并导致重新显示表单。在这种情况下,由于字段的已修正数据标记仍设置为 yes,所以,当操作键被按下时,验证器函数将再次执行。

设置 modified 属性

您可能想让程序在无论用户是否已修改特定字段的情况下都执行某项任务;例如:
  • 您可能想强制验证密码字段,即使用户未在该字段中输入数据亦如此
  • 可以对关键字段(甚至是对受保护字段)指定验证函数,以便程序始终执行特定交叉字段验证,这表示程序逻辑验证一组字段并考虑一个字段的值是如何影响另一字段的有效性的。
要处理上述情况,可以在程序逻辑中或在表单声明中对特定项设置 modified 属性:
  • 在表单显示前的逻辑中,包括一个 set item modified 类型的语句。结果是,当表单显示时,该项的已修正数据标记已被预设为 yes
  • 在表单声明中,将项的 modified 属性设置为 yes。在这种情况下,下列规则适用:
    • 当表单第一次显示时,该项的已修正数据标记被预设为 yes
    • 如果在表单显示前发生下列任何情况,则表单显示时,已修正数据标记会被预设为 yes
      • 代码运行了 set item initial 类型的语句,该语句重新指定项的最初内容和属性值;或者
      • 代码运行了 set item initialAttributes 类型的语句,该语句重新指定表单上的每个项的最初属性值(但未指定内容);或者
      • 代码运行了 set form initial 类型的语句,该语句重新指定表单上的每个项的最初内容和属性值;或者
      • 代码运行了 set form initialAttributes 类型的语句,该语句重新指定表单上的每个项的最初属性值(但未指定内容)

set 语句影响 modified 属性的值,但不影响已修正数据标记的当前设置。if item modified 类型的测试基于上次将表单数据返回至程序时正在起作用的已修正数据标记值。如果在逻辑第一次显示表单之前尝试测试项的已修正数据标记,则在运行时会发生错误。

如果需要检测用户(而不是程序)是否已修改了某个项,则确保将该项的已修正数据标记值预设为 no
  • 如果在表单声明中将项的 modified 属性设置为 no,则不要使用 set item modified 类型的语句。如果不存在该语句,则会在每次显示表单前将 modified 属性自动设置为 no
  • 如果在表单声明中将项的 modified 属性设置为 yes,则在先于表单显示的逻辑中使用 set item normal 类型的语句。该语句将 modified 属性设置为 no 并(作为间接结果)将该项作为未受保护并具有正常强度的项显示。

测试表单是否已被修改

如果对任何变量表单项将已修正数据标记设置为 yes,则将整个表单视为已被修改。如果测试尚未向用户显示的表单的已修改状态,则测试结果是 FALSE。

示例

假定表单 form01 具有下列设置:
  • 字段 item01modified 属性设置为 no
  • 字段 item02modified 属性设置为 yes

以下逻辑显示了各种测试的结果:

  // tests false because a converse statement
  // was not run for the form
  if (form01 is modified)
    ;
  end

  // causes a run-time error because a converse
  // statement was not run for the form
  if (item01 is modified)
    ;
  end

  // assume that the user modifies both items
  converse form01;

  // tests true
  if (item01 is modified)
    ;
  end

  // tests true
  if (item02 is modified)
    ;
  end

  // sets the modified property to no
  // at the next converse statement for the form
  set item01 initialAttributes;
  
  // sets the modified property to yes
  // at the next converse statement for the form
  set item02 initialAttributes;

  // tests true
  // (the previous set statement takes effect only
  // at the next converse statement for the form
  if (item01 is modified)
    ;
  end

  // assume that the user does not modify either item
  converse form01;

  // tests false because the program set the modified
  // data tag to no, and the user entered no data
  if (item01 is modified)
    ;
  end

  // tests true because the program set the modified
  // data tag to yes
  if (item02 is modified)
    ;
  end
  
  // assume that the user does not modify either item
  converse form01;

  // tests false
  if (item01 is modified)
    ;
  end

  // tests false because the presentation was not
  // the first, and the program did not reset the
  // item properties to their initial values
  if (item02 is modified)
    ;
  end
使用条款 | 反馈
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.