🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Data validation
This page was created by Stephanie on 2024-02-22. Last edited by Wikiadmin on 2026-07-29.

You can define data rules in MDriven Designer to keep model data meaningful and to give users targeted feedback in a specific ViewModel.

Choose the right kind of validation

Data validation checks whether entered data follows a rule. In MDriven, use the level that matches the rule's purpose:

Use this when Define it in Example
The rule is a business rule for an object wherever that object is used A class constraint in the model A Task priority must be from 1 through 5.
The rule is relevant to one screen or use case and you want to mark a particular input control A validation rule in the ViewModel In a rental form, show an error beside RegistrationNumber when it has fewer than six characters.

A class constraint expresses a rule on the model. A ViewModel validation expresses a rule for a use case, because a use case is implemented by a ViewModel. Do not duplicate the same business expression in both places. When a ViewModel needs to reflect a class constraint, look up the constraint and test whether it is not broken instead of rewriting its expression. See ViewModel validations for that pattern.

Write validation expressions in OCL

MDriven validation expressions use Object Constraint Language (OCL). A validation expression must evaluate to true when the rule is fulfilled and false when the data breaks the rule.

Rule OCL expression Suggested message
A first name is required self.FirstName.IsEmpty implies "FirstName is required" FirstName is required
A day of birth is in the expected range (self.DayOfBirth > 0) and (self.DayOfBirth < 32) Day of birth must be between 1 and 31
A task priority is from 1 through 5 (self.Priority >= 1) and (self.Priority <= 5) Priority must be between 1 and 5
An email address matches a specified format self.EmailAddress.regExpMatch('^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$') Enter a valid email address

Choose the correct type when you create a model attribute, such as String, Date, DateTime, Decimal, Float, or Blob. Attribute types define what kind of value the model holds; OCL rules define additional conditions such as a range, required value, format, or condition that depends on another value.

Add a ViewModel validation rule

Use a ViewModel validation when you want a field-level message in a particular UI. The rule context is the Main ViewModelClass, so write the expression from that context and reach the relevant variables or values from there.

  1. Open the ViewModel in the ViewModel Editor.
  2. Right-click in the validation area and select Add Validation.
  3. In the new row, enter a descriptive rule name.
  4. Enter an OCL expression that returns true when the entered data is valid.
  5. Enter the message that the user should see when the expression returns false.
  6. Associate the rule with the input column: right-click the relevant ViewModelColumn, select Validation rules, and select the rule.
  7. Refresh or run the application and enter a value that breaks the rule to verify the message and styling.

You can associate one rule with multiple columns. This is useful when a condition concerns more than one input. For example, a rule that checks a selected rental contract can be associated with the ApartmentNumber column so the user sees where to act.

Example: registration number length

In a ViewModel whose Main ViewModelClass exposes RegistrationNumber, create a rule such as:

self.RegistrationNumber.Length > 5

Set the message to:

Registration number must contain at least 6 characters

Then associate the rule with the RegistrationNumber ViewModel column. When the value has five or fewer characters, the rule is false and the UI can display the validation feedback on that column.

Understand validation feedback

When an associated ViewModel rule is broken at runtime, MDriven produces a standard WPF ValidationError. How it appears depends on the active UI style. The default WPF styling can show a red border, an exclamation mark, and a tooltip containing the validation message. Web interfaces also apply the ViewModel validation associations.

Feedback timing can differ by UI. In the referenced WPF example, field validation is applied when the user leaves the cell; web behavior can update while the user types. Test the target UI and style with the interaction your users will perform.

A ViewModel validation is feedback for that use case. It does not by itself make a business rule universal, and the UI may still ask whether to save when errors are present. Put rules that must hold for the object itself in a model constraint, then use ViewModel validation to direct the user to the relevant field when appropriate.

Work with constraints deliberately

Constraints discovered from classes used by a ViewModel can be shown as informational messages. Whether a constraint should be visible depends on the use case. For example, do not show a message demanding an apartment number in a ViewModel where the user cannot enter an apartment number.

Constraints can have levels such as error and warning, which can be rendered differently by the UI style. Use a warning when users should be informed but the condition is not treated as an error in that use case. For a walkthrough of opting constraint feedback in or out and adding targeted ViewModel rules, see Documentation:Turnkey session 4: ViewModel validation.

Test validation rules

  1. Test a valid value and confirm that the rule evaluates to true and no message is shown.
  2. Test an invalid value and confirm that the intended message appears on every associated column.
  3. Test empty values, boundary values, and values that affect conditional rules.
  4. Test every ViewModel that exposes the same object when the rule is a model constraint.
  5. Save the model and use the Validation button, or save to run model verification automatically. Resolve model errors before prototyping or deployment; see MDrivenStart:MDrivenStart Verify.

For an end-to-end priority validation example in a task-planner model, see How to Build a Team Task Planner in 15 Minutes with MDriven. For validation of OCL supplied dynamically as text in an EAL action, use ScriptEvalCheck rather than executing unchecked text.

See also