Example E1524
Visible to All Users

XAF - How to Highlight Invalid Properties Immediately in an Invoked View

The XAF Validation Module usually validates data when a user saves or deletes an object. This example demonstrates how to check validation rules when a user activates a View or changes the View's object.

image

Files to Look At

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

EFCore/ValidateHighlightEF/ValidateHighlightEF.Module/Controllers/ImmediateValidationController.cs
C#
using System; using System.ComponentModel; using System.Collections.Generic; using System.Diagnostics; using System.Text; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Actions; using DevExpress.Persistent.Base; using DevExpress.ExpressApp.Validation; using System.Collections; using DevExpress.Persistent.Validation; using DevExpress.ExpressApp.Editors; namespace ValidateHighlight.Module { public class ImmediateValidationTargetObjectsSelector : ValidationTargetObjectSelector { protected override bool NeedToValidateObject(IObjectSpace objectSpace, object targetObject) { return true; } } public class ImmediateValidationController : ViewController { protected override void OnActivated() { base.OnActivated(); ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged; ObjectSpace.ObjectReloaded += ObjectSpace_ObjectReloaded; View.CurrentObjectChanged += View_CurrentObjectChanged; } protected override void OnViewControlsCreated() { base.OnViewControlsCreated(); ValidateViewObjects(); } void View_CurrentObjectChanged(object sender, EventArgs e) { ValidateViewObjects(); } private void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) { ValidateViewObjects(); } private void ObjectSpace_ObjectReloaded(object sender, ObjectManipulatingEventArgs e) { ValidateViewObjects(); } private void ValidateViewObjects() { if (View is ListView) { if (!((ListView)View).CollectionSource.IsServerMode) { ValidateObjects(((ListView)View).CollectionSource.List); } } else if (View is DetailView) { ImmediateValidationTargetObjectsSelector objectsSelector = new ImmediateValidationTargetObjectsSelector(); ValidateObjects(objectsSelector.GetObjectsToValidate(View.ObjectSpace, View.CurrentObject)); } } private void ValidateObjects(IEnumerable targets) { if (targets == null) return; ResultsHighlightController resultsHighlightController = Frame.GetController<ResultsHighlightController>(); if (resultsHighlightController != null) { IRuleSet ruleSet = Validator.GetService(Application.ServiceProvider); RuleSetValidationResult result = ruleSet.ValidateAllTargets(ObjectSpace, targets, DefaultContexts.Save); if (result.ValidationOutcome == ValidationOutcome.Error || result.ValidationOutcome == ValidationOutcome.Warning || result.ValidationOutcome == ValidationOutcome.Information) { resultsHighlightController.HighlightResults(result); } else { resultsHighlightController.ClearHighlighting(); } } } protected override void OnDeactivated() { base.OnDeactivated(); ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged; ObjectSpace.ObjectReloaded -= ObjectSpace_ObjectReloaded; View.CurrentObjectChanged -= View_CurrentObjectChanged; } } }
EFCore/ValidateHighlightEF/ValidateHighlightEF.Module/BusinessObjects/TestObject.cs
C#
using System; using DevExpress.ExpressApp; using DevExpress.Persistent.Base; using DevExpress.Persistent.BaseImpl; using DevExpress.Persistent.Validation; using DevExpress.ExpressApp.Editors; using DevExpress.Persistent.BaseImpl.EF; namespace ValidateHighlight.Module { [DefaultClassOptions] public class TestObject : BaseObject { [RuleRequiredField(ResultType = ValidationResultType.Error)] public virtual string Error { get; set; } [RuleRequiredField(ResultType = ValidationResultType.Warning)] public virtual string Warning { get; set; } [RuleRequiredField(ResultType = ValidationResultType.Information)] public virtual string Information { get; set; } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.