Field validation hook example

Use Validation hooks to verify that a field contains an appropriate value. Validation hooks are called at predefined times to verify that the field contents are valid. If a record contains any fields with values that are not valid, the record is not committed to the database until the error is corrected.

The advantage of using hooks to validate individual fields (as opposed to using an action validation hook to validate the entire record) is that the user is notified immediately if the field value is not valid.

Field validation hooks are written as functions that return a string value. The function return value is considered to be an error message. If the return value is an empty string, the field value is considered valid.

In the following example, if fewer than 10 characters are typed in the field, the validation hook rejects the entry, forcing the user to type at least 10 characters.

VBScript


Function word_Validation(fieldname)

    ' fieldname As String
    ' word_Validation As String
    ' entityDef = puzzle_words

    Dim val

    val = GetFieldValue(fieldname).GetValue()
    If Len(val) < 10 Then
      word_Validation = "All words must be at least 10 letters long"
    End If

End Function 

Perl


sub word_Validation  {

    my($fieldname) = @_;

    # $fieldname as string scalar
    # $result as string scalar
    # $entityDef = puzzle_words

    my($value);

    $value = $entity->GetFieldValue($fieldname)->GetValue();

    if (length ($value) < 10)  {
      $result = "All words must be at least 10 letters long";
    }
    return $result;
}