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 should meet. For example, with validation you can ensure 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 VBScript Variables Explained.

Complete the following steps to add value validation for a property:

Steps

  1. Open M-Files Admin.
  2. In the left-side tree view, expand the desired connection to M-Files Server.
  3. In the left-side tree view, expand the document vault of your choice.
  4. Still in the left-side tree view, expand Metadata Structure (Flat View) and then select Property Definitions.
  5. Either:
    1. In the Property Definitions list, right-click the property, the values of which you want to be automatically validated, and select Properties from the context menu.
      or
    2. Click New Property Definition... on the task area to create a new property definition with automatic value validation.
    The Property Definition Properties dialog is opened.
  6. Go to the Validation tab.
    The Validation tab is opened.
  7. Select the Validation with VBScript option and click the Edit Code... button.
    The Edit VBScript Code window is opened.
  8. In the Edit VBScript Code window, type in the VBScript code for validating the values of this property.
    For 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 documentation for the M-Files API is located in Start > Programs > M-Files > Documentation > M-Files API. For more information about VBScript code and M-Files API, go to www.m-files.com/api. Instructions on writing VBScript code and working with the M-Files API are available for a separate fee from M-Files customer support ([email protected]).
  9. Close the Edit VBScript Code window and then click Apply in the Property Definition Properties dialog to save your changes.

Results

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).