Skip to main content
A newer version of this page is available. .

RuleBaseAttribute.CustomMessageTemplate Property

Specifies informational text to be added to the exception message, when the current rule is broken.

Namespace: DevExpress.Persistent.Validation

Assembly: DevExpress.Persistent.Base.v18.2.dll

Declaration

public string CustomMessageTemplate { get; set; }

Property Value

Type Description
String

A string value representing information defining the current rule.

Remarks

When rules are broken, an error window is invoked. It details why each rule is broken. This information is provided by the default message templates specified for each rule type. You can see them in the Model Editor’s IModelValidationDefaultErrorMessageTemplates node. You can customize these default messages directly in this node. In this instance, your messages will be used for all rules of a certain type. In addition, you can set a custom message template for a particular rule using the Validation | Rules | Rule node’s CustomMessageTemplate property, or any of the MessageTemplate… properties. To set a custom message for a particular rule in code, use the CustomMessageTemplate parameter of the rule attribute. In the message, you can use format items. For instance, the “RightOperand” format item is used in the following code:

using System.ComponentModel;
//...
public class Account : BaseObject {
   //...
   private double amount;
   [RuleValueComparison("RuleRequiredField for Account .Amount", DefaultContexts.Save,
      ValueComparisonType.LessThan,100,
      CustomMessageTemplate = "The Amount must not be less than {RightOperand}.")]
   public double Amount { 
      get { 
         return amount; 
      }
      set { 
         SetPropertyValue("Amount", ref amount, value);
      }
   }
}

There is a RightOperand property in the Application Model Node that defines RuleValueComparison rules. So, we used the “RightOperand” format item in the message template in the code above. You can use any property of the appropriate node as a format item for a message template. In addition, use the following format items:

  • TargetObject

    Returns the value of the validated object’s key property.

  • TargetValue

    Returns the value of the validated property.

  • <The Object’s Property Name>

    Returns the value of the property.

The message template specified for a validation rule in code is assigned to the CustomMessageTemplate property of the appropriate IModelRuleBase node. So, you can customize the description directly in the Model Editor.

When using the TargetPropertyName format item, the value of the corresponding BOModel | <Class> | OwnMembers | <Member> node’s Caption property will be used in the message template. Moreover, if you specified a localized version of the Caption property, it will be inserted in the message template when the corresponding language is used in the application.

Note

Be careful when setting a value to the CustomMessageTemplate for the RuleUniqueValueAttribute or inverted RuleIsReferencedAttribute and RuleObjectExistsAttribute. In this instance, the list of found objects will not be included in the message. To display this list, use the MessageTemplateMustBeUnique property (in the Model Editor), RuleIsReferencedAttribute.MessageTemplateMustBeReferenced or RuleObjectExistsAttribute.MessageTemplateMustExist respectively, instead.

See Also