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 Available VBScript Variables.
By default, validation is considered successful. Invalid values are thus detected using conditional statements and should any of the conditions specified in the validation be met, then an error should be raised, prompting the user to correct the invalid value (for instance, Err.Raise MFScriptCancel, "The property must have a value of at least 10 characters.").
Complete the following steps to add value validation for a property:
- Open M-Files Admin.
- In the left-side tree view, expand a connection to M-Files server.
- Expand Document Vaults.
- Expand a vault.
- Expand Metadata Structure (Flat View).
- Click Property Definitions.
-
Either:
- 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
- In the task area, click New Property Definition to create a new property definition with automatic value validation.
- 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.
- Go to the Validation tab.
- Select the Validation with VBScript option and click the Edit Code... button.
-
In the Edit VBScript Code window, type in 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
- Close the Edit VBScript Code window and then click Apply in the Property Definition Properties dialog to save your changes.