Please enable JavaScript to view this site.

Codolex

You can see in the generated code above that the assumption is made that a SoundFile object is always available. The user of the activity has to provide this file, but it may very well be that the user forgets to provide one, or deletes the variable that was being used.

 

To prevent errors in that case, we need to add some validation. We are going to validate if the soundfile variable is selected in the activity. This can be done in the Codolex.Activity.YourActivityName.Validator file. This file provides a DoValidate function to Codolex to execute when the validation is called. This function has the Variable FElement available which will be the instance of your component when validating. You can use this element to perform all kinds of validations, like if a property in the component is filled, when working with numbers in inputs, if the numbers are correct and within limits, etc.

 

In the example, an error is thrown when the validator is not changed. However, let's now change the validator to remove the error. Let's also check if the SoundFile property is filled, so we know the generated code won't give an error.

resourcestring
  NoSoundFileProvided = 'No sound file provided for %s';

 
procedure TFlowActivityPlaySoundValidator.DoValidate;
begin
  inherited;
  if not Assigned(FElement.SoundFile) then
    AddError(NoSoundFileProvided, [FElement.ComponentName], 'SoundFile');
end;

 

.AddError

When the property is not filled, we can add an error to the validator with the "AddError" function, This function can make use of the format functionality. Be sure to also provide the name of the property that the error is for. This is not used for now but can be used in the future to show more specific error messages or show them in the right place in the editor.

 

When an error is added for a component, the code generation will not execute, and an error is shown at the place where the code should be.

 

.AddWaring

 

The AddWarning function can also be used to let the user of the activity know that something might not be right or could be improved. For now, this will also prevent the code from generation, but this could be removed in the future.

var Soundpath: string;
Soundpath := 'file';
{$MESSAGE WARN '"Play sound": No sound file provided for Template.Core.PlaySound'}

 

© by GDK Software