🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Explaining “The ViewModel does not require a root object” warning
This page was created by Alexandra on 2018-11-02. Last edited by Wikiadmin on 2026-07-29.

You can use this warning to align a ViewModel's Requires root setting with the action that opens it, so that rooted views receive an object and unrooted views do not.

What the warning means

A ViewModel can be either rooted or unrooted. The Requires root checkbox declares which form the ViewModel expects. An action that opens the ViewModel must match that declaration:

ViewModel intent Requires root Action configuration Typical use
Rooted Selected Set a ViewModel root-object expression that supplies an object. Show one object and information related to it.
Unrooted Cleared Leave the ViewModel root-object expression empty. Show or search across a class of objects.

The warning does not say that rooted or unrooted ViewModels are wrong. Both are valid. It identifies a mismatch between what the ViewModel declares and what the opening action supplies.

Choose the intended ViewModel type

Use a rooted ViewModel for one object

Use a rooted ViewModel when the view starts with one specific model object. The root ViewModel class has that object as its context, so self in the root ViewModel class refers to the supplied root object.

For example, a ViewModel named ViewOneThing can receive one Thing. Its root ViewModel class can use self to show attributes of that Thing and navigate to related details.

  1. Open the ViewModel in MDriven Designer.
  2. Set its root object class to the model class that the view will receive, for example Thing.
  3. Select Requires root.
  4. In every action that opens this ViewModel, set the ViewModel root-object expression to the object to display. For an action in the context of a Thing, that expression can be self.

A child ViewModel class can have a different context. For example, when a child class contains the Thing's details, self in that child class refers to a detail object, not to the root Thing. See Documentation:ViewModel variables for the context-dependent meaning of self and the available ViewModel variables.

Use an unrooted ViewModel for a list or search

Use an unrooted ViewModel when the view does not begin with one specific object. This is common for a browser or seeker that lists objects from a class.

For example, a ViewModel named ViewAllThings can populate a list from Thing.allInstances. The root ViewModel class has no supplied model-object context, so it must not depend on self as a root object. The list's ViewModel class does have a context for each listed item; within that class, self refers to the individual item.

  1. Open the ViewModel in MDriven Designer.
  2. Set the root object class to communicate the class the ViewModel concerns, for example Thing.
  3. Clear Requires root.
  4. In each action that opens this ViewModel, leave the ViewModel root-object expression empty.

An unrooted ViewModel is appropriate for showing all objects, a filtered subset, or search results. For the broader rooted/unrooted design guidance, see Documentation:ViewModel.

Fix the reported mismatch

Warning: the ViewModel requires a root, but the action supplies none

You may see a message similar to:

WARNING: If the ViewModel requires a root object [RequiresRootObject on ViewModel] you should not have it empty ViewOneThing

This means the target ViewModel has Requires root selected, but the action that opens it has no ViewModel root-object expression.

Fix it in one of two ways:

  1. If the view is intended to show one object, keep Requires root selected and set the action's ViewModel root-object expression. For example, use self when the action is executed in the context of the object to open.
  2. If the view is intended to be a browser or seeker, clear Requires root and make sure the root ViewModel class does not require a supplied root object.

Do not use an Execute expression on show as a substitute for the root-object expression. An expression that runs when the view shows can initialize state, but the root-object expression is what supplies the root when opening a rooted ViewModel.

Warning: the ViewModel does not require a root, but the action supplies one

You may see a message similar to:

WARNING: The ViewModel does not require a root object [RequiresRootObject on ViewModel] but there is a Expression in ViewModelRootObjectExpressionViewOneThing

This means the target ViewModel has Requires root cleared, but the action has a ViewModel root-object expression.

Fix it in one of two ways:

  1. If the ViewModel is intended to open on a selected object, select Requires root on the ViewModel. Confirm that its root object class and root-level expressions are designed for that object.
  2. If the ViewModel is intended to be unrooted, remove the ViewModel root-object expression from the action.

For example, an action that opens ViewAllThings should not pass self. The view finds and displays its own collection instead of receiving one Thing as its starting point.

Check the configuration step by step

  1. Open the ViewModel named by the warning.
  2. Decide whether users should open it for one specific object or for a class-wide list/search.
  3. Check the ViewModel's root object class and its Requires root checkbox.
  4. Review the root ViewModel class expressions:
    • A rooted root class can use self for the supplied root object.
    • An unrooted root class must not rely on self as a supplied root object.
  5. Open each action that leads to the ViewModel.
  6. Compare the action's ViewModel root-object expression with the checkbox setting.
    • Requires root selected: the expression must supply the object to open.
    • Requires root cleared: the expression must be empty.
  7. Save the model and run model checking again.

The ViewModel editor can show actions leading to the ViewModel, which helps you find every action that must be reviewed. AutoForm-generated actions can also participate in this check; changes to generated ViewModels or actions may be replaced when AutoForms are regenerated. See Training:Bootcamp:Chapter 3 for an example of correcting a root requirement mismatch in an AutoForm scenario.

Root-object access in OCL

When you need to test whether the current running ViewModel has a root object, use selfVM.RootObject. For example:

selfVM.RootObject->notEmpty

RootObject is not statically typed. Cast it before using attributes of the actual root type, for example:

selfVM.RootObject->safeCast(Thing).SomeString := 'a new value'

This operator does not change the opening action's responsibility to supply a root for a rooted ViewModel. Read Documentation:OCLOperators RootObject for the operator's behavior and type requirements.

Related checks

A correctly supplied root object does not by itself grant access to a ViewModel. If the ViewModel has an access expression, you can evaluate object.canAccess(ViewModel) before opening it; see Documentation:ViewModel access and security.

See also