Automatically validating property values

On the Validation tab of the Property Definition Properties dialog, you can define the criteria that the values of a specific property must meet. For example, with validation you can make sure that the property value contains a required number of characters. In this way, you can verify that the customer phone number or invoice number is added correctly on the metadata card. You can also validate that, for instance, the value can be accepted in relation to other properties or that the value is not empty.

Validation is specified by using variables, generic features of VBScript, and M-Files API. The following M-Files variables can be used for validating property values: PropertyDef, PropertyValue, ObjVer, DisplayID, Vault, CurrentUserID, CurrentUserSessionInfo, VaultSharedVariables, SavepointVariables, TransactionCache, MFScriptCancel, GetExtensionObject, MasterTransactionID, CurrentTransactionID, ParentTransactionID. For more information about the variables, refer to Available VBScript variables.

Important: When you develop new extensions or edit existing ones, we recommend that you replace VBScript content with Vault Application Framework (VAF) compatible code for future compatibility. For information on the benefits of VAF over VBScript, refer to The Vault Application Framework in M-Files Developer Portal.

By default, validation is considered successful. Invalid values are thus detected with conditional statements. If any of the conditions specified in the validation is met, an error is raised. The error prompts the user to correct the invalid value. For example, Err.Raise MFScriptCancel, "The property must have a value of at least 10 characters.".

To add value validation for a property:

  1. Open M-Files Admin.
  2. In the left-side tree view, expand a connection to M-Files server.
  3. Expand Document Vaults.
  4. Expand a vault.
  5. Expand Metadata Structure (Flat View).
  6. Select Property Definitions.
  7. Either:
    • In the Property Definitions list, open the context menu of the property, the values of which you want to be automatically validated, and select Properties from the context menu.
      or
    • In the task area, select New Property Definition to create a new property definition with automatic value validation.
    Result:The Property Definition Properties dialog is opened.
  8. Go to the Validation tab.
    Result:The Validation tab is opened.
  9. Select the Validation with vault application or VBScript option, and then Details.
    Result:The VBScript - Details window is opened.
  10. Enter the VBScript code for validating the values of this property.
    Example:If the values of this property must have at least 10 characters, you could use the following code:
    Option Explicit
    
    Dim propertyName, value
    
    propertyName = PropertyDef.Name
    
    value = PropertyValue.GetValueAsUnlocalizedText
    
    If Len(value) < 10 Then
    
        Err.Raise MFScriptCancel, "The property """ & propertyName & """ must have a value of at least 10 characters."
    
    End If
    Note: The M-Files API documentation is available online: M-Files API.
  11. Close the VBScript - Details window and select Apply in the Property Definition Properties dialog.
The values entered for the selected property are now automatically validated. When entering a value for the property on the metadata card, the value is validated and if it does not meet the criteria specified, the action specified in the validation script is executed (such as displaying an error message).