ViewModel Validations in MDriven Turnkey
ViewModel validations in MDriven Turnkey are essential for ensuring data integrity and providing users with immediate feedback when data doesn't meet predefined criteria. This tutorial covers adding, associating, and testing validation rules in MDriven Turnkey.
Adding Validation Rules in MDriven Turnkey
To add validation rules to a ViewModel, follow these steps in the ViewModelEditor:
- Open your ViewModel in the MDriven Designer.
- In the Validations section, click to add a new validation rule.
- Provide a name for your rule.
- Enter an OCL expression that evaluates to true when the rule is satisfied and false when it is not. For example, to ensure a registration number is longer than 5 characters, use:
self.RegistrationNumber.size() > 5
- Specify the error message that should be displayed when the rule is broken.
The context of the rule is the Main ViewModelClass, allowing you to access variables and attributes directly.
Associating Validation Rules with Columns
Once you've defined your validation rules, you can associate them with specific ViewModelColumns:
- Right-click on a ViewModelColumn.
- Check the appropriate validation rule to link it to the column.
When a rule is violated, a standard WPF ValidationError will occur, styled according to your application's design.
Testing Validation Rules
After setting up your validation rules, upload the model to your MDriven Turnkey application and test the rules:
- Open the application and navigate to the relevant ViewModel.
- Enter data that violates the validation rule, such as a registration number shorter than 6 characters.
- Observe the validation message displayed, confirming the rule is active.
WPF Implementation
In WPF, a default style is applied to highlight validation errors. This style can be customized to fit your application's theme:
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<Border BorderBrush="Red" BorderThickness="2" CornerRadius="4,4,4,4">
<AdornedElementPlaceholder/>
</Border>
<TextBlock Foreground="Red" VerticalAlignment="Center" FontWeight="ExtraBold" Margin="4">!</TextBlock>
</DockPanel>
</ControlTemplate>
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource validationTemplate}">
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="TextBox.ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
When a validation rule is broken, it appears as shown below:
Linking Constraints with ViewModel Validations
For effective validation, ensure that constraints in your class are not duplicated in the ViewModel. Instead, use methods to verify constraints by name, returning a boolean to indicate validity. This approach links class constraints with ViewModel validations, ensuring consistency across your application.
For more on ViewModel validations, see ViewModel validations. To explore how validation rules differ from constraints, refer to Turnkey session 4: ViewModel validation.
Source
Based on the MDriven video MDriven Turnkey | Part 4 | ViewModel validation.
