Skip to main content

Sequential validation when editing a model

Overview

This is a tutorial on using the Next Design extension to receive model operations, file operations, etc. as events and to implement unique processing for each event.
Based on the extension created in the previous tutorial: Bulk model validation, let's expand the functionality to validate the model sequentially according to the model editing operation.

Overall flow

  • Defining event extension points using a manifest
  • Implementing sequential validation processing

Public sample

  • The complete set of source code created as a result of this tutorial is available on GitHub.

External link: ValidationSample-2

Goal image

Screen capture or GIF animation

  • Open the [Advanced Driving System Software Development] project from the Next Design sample projects.

  • If you select any model and change the name of the model, the model will be validated according to the following validation rules. If a validation rule is violated, the result will be displayed as an error.

Validation rule: The model name does not contain half-width spaces.

info
  • If you cannot find [Advanced Driving System Software Development], install the extension BasicSamples package.
    For information on installing official extensions, see here.

Defining an extension point for an event in the manifest

To define an extension point, define the following in the manifest:

  • Defining an extension point for an event

Implementation example

The name of a model is a type of field that a model has. Therefore, a name change can be received as a field change event for the model.
To receive a field change event for the model and call the event handler, define the following in the manifest:

manifest.json

{ 
//Extension definition

//Extension point definition
"extensionPoints": {
//Ribbon
"ribbon": {
},

//Commands
"commands": [
],

//Events
"events": {
//Add an extension point for model-related events.
"models": [
{
"class": "*", //To perform validation processing for all models, specify "*" in the event filter or omit this line.
//To perform validation processing for only specific classes, list the target class names in the event filter, separated by commas.
//"class": "TechnicalArchitectureComponentECU,TechnicalArchitectureComponentSensor",
"onFieldChanged": "OnFieldChanged" //Register the public method implemented in the main class of the entry point as an event handler when the model field is changed.
}
]
}
}
}
note
  • The implementation example is written with a focus on the functional extension part from Bulk model verification. Therefore, please note that the implementation example will not work on its own.

Implementing sequential verification processing

To implement sequential verification processing when the model field is changed, implement the following additional content in the class.

  • Validation processing event handler (public method)

Implementation example

To check the event details and call name verification processing in the event handler called from the event, implement the following content as a public method.

ValidationSample.cs

//Declaration of API namespaces referenced in extension development 
using NextDesign.Extension;
using NextDesign.Core;
using NextDesign.Desktop;

///<summary>
///Entry point DLL main class
///</summary>
///<remarks>
///The entry point main class implements the `IExtension` interface.
///Handlers are implemented as public methods of this class.
///</remarks>
public class ValidationSample : IExtension
{
///<summary>
///Event handler for model field change
///</summary>
///<param name="context">Event context</param>
///<param name="eventParams">Event parameters</param>
///<remarks>
///Implement the model field change event handler as a public method of the main class.
///</remarks>
public void OnFieldChanged(IEventContext context, IEventParams eventParams)
{
//Detailed information such as the model whose field was changed and the field name is obtained from the event parameters passed to the second argument of the event handler.
//The specific type of the event parameters differs depending on the type of event, so they are type-converted to the event parameters for the model's field change event.
var fieldChangeParams = eventParams as ModelFieldChangedEventParams;
var model = fieldChangeParams.Model; //Model with changed field
var fieldName = fieldChangeParams.Field; //Name of the changed field
var app = context.App;

//Validate only if the changed field is the `Name` field.
if (fieldName == "Name")
{
//Clear previous errors for the target model.
app.Errors.ClearErrorsAt(model);

//Validate the model according to the validation rules.
ValidateModel(model);
}
}
}
note
  • The implementation example is written with a focus on the functional extension part from Bulk model validation. Therefore, please note that the implementation example will not work on its own.