Field-Map Scripting

2.5.2 On-Validate scripts

On-Validate scripts


This kind of a script is bound to a single field and it is executed BEFORE the value of that field changes. The purpose of an On-Validate script is to verify whether the new value is valid or not. You can access values of other fields in your script, interact with the user or do whatever else to determine if the value is valid or not.

If the value is found to be valid, set a predefined variable "ScriptResult" to "1" in the script. After the script has finished executing, the new value is accepted and stored in a database and the On-Change script is executed (if there is any).

If the new value is decided to be invalid, set the predefined variable "ScriptResult" to "0" in your script. When the script finishes executing, the new value is rejected (not stored in the database) and the On-Change script is not executed (even if there is one).

If the the "ScriptResult" variable is set to "0" (that is when the newly added value is recognized to be invalid), a warning message box always appears to inform the user about the validation failure.

You can optionally use a predefined variable "ValidationFailedMessageText" to customize the message to be displayed in this warning message box.

You can also turn off the default message display. To do this, set ValidationFailedMessageText:='Nomessage'.

Note: You should not change values of other fields in an On-Validate script, use an On-Change script instead.


Example:

The following script checks the DBH value before it is saved into a database and rejects the value if it exceeds predefined value.

{limiting values of DBH} 

const
  RS_error1='Unacceptable value of DBH (%0.1f cm).';

begin
  if Trees['DBH_cm']>200 then begin 
    ValidationFailedMessageText := format(RS_error1,[double(Trees['DBH_cm'])]);
    ScriptResult:=0;
  end;
  
end.


Another example checks the height just inserted with the one in the previous measurement, and if it's smaller, or if the annual growth is more than 70 cm, it displays its own warning message.

var OldH_,NewH_,LastInventoryYear_ :variant;
    Interval_ :integer;
begin
  if AttributeNewValue=Unassigned then NewH_:=Trees2['Height']
  else NewH_:=AttributeNewValue;
  if (NewH_<>NULL) and (NewH_<>0) then begin
    OldH_:=Trees2.OldValue['Height'];                                                     
    if (OldH_<>null) and (OldH_<>Unassigned) then begin
      LastInventoryYear_:=Plots.GetAttributeValue('LastInventoryYear','IDPlots<>0');
      Interval_:=Year-LastInventoryYear_;
      if NewH_<OldH_ then begin
        if not YesNoQuestion(format( 'New height is smaller than previous (%0.0f->%0.0f).',[double(OldH_),double(NewH_)])
                                    +#13'Accept anyway ?') then
        begin
          ScriptResult:=0;
          ValidationFailedMessageText:='NoMessage';
        end;
      end
      else if (NewH_-OldH_)/Interval_*5>70 then begin 
        if not YesNoQuestion(format( 'Height increment is too high (%0.0f->%0.0f).',[double(OldH_),double(NewH_)])
                                    +#13'Accept anyway ?') then
        begin
          ScriptResult:=0;
          ValidationFailedMessageText:='NoMessage';
        end;
      end;
    end;
  end;  
end.


See also:                

Field-Map script types overview; On-Validate scripts; On-Change scripts; On-Demand scripts; Layer scripts; Global scripts; Field-Map script types; Writing scripts in Field-Map