CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
EditContextCslaExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Text;
4using Csla.Core;
5using Csla.Rules;
6using Microsoft.AspNetCore.Components.Forms;
7
8namespace Csla.Blazor
9{
13 public static class EditContextCslaExtensions
14 {
19 public static EditContext AddCslaValidation(this EditContext editContext)
20 {
21 if (editContext == null)
22 {
23 throw new ArgumentNullException(nameof(editContext));
24 }
25
26 var messages = new ValidationMessageStore(editContext);
27
28 // Perform object-level validation on request
29 editContext.OnValidationRequested +=
30 (sender, eventArgs) => ValidateModel((EditContext)sender, messages);
31
32 // Perform per-field validation on each field edit
33 editContext.OnFieldChanged +=
34 (sender, eventArgs) => ValidateField(editContext, messages, eventArgs.FieldIdentifier);
35
36 return editContext;
37 }
38
45 private static void ValidateModel(EditContext editContext, ValidationMessageStore messages)
46 {
47 ICheckRules model;
48
49 // Get access to the model via the required interface
50 model = editContext.Model as ICheckRules;
51
52 // Check if the model was provided, and correctly cast
53 if (editContext.Model == null)
54 {
55 throw new ArgumentNullException(nameof(editContext.Model));
56 }
57 if (model == null)
58 {
59 throw new ArgumentException(
61 nameof(editContext.Model), nameof(ICheckRules)));
62 }
63
64 // Transfer broken rules of severity Error to the ValidationMessageStore
65 messages.Clear();
66 foreach (var brokenRuleNode in BusinessRules.GetAllBrokenRules(model))
67 {
68 foreach (var brokenRule in brokenRuleNode.BrokenRules)
69 if (brokenRule.Severity == RuleSeverity.Error)
70 {
71 // Add a new message for each broken rule
72 messages.Add(new FieldIdentifier(brokenRuleNode.Object, brokenRule.Property), brokenRule.Description);
73 }
74 }
75
76 // Inform consumers that the state may have changed
77 editContext.NotifyValidationStateChanged();
78 }
79
87 private static void ValidateField(EditContext editContext, ValidationMessageStore messages, in FieldIdentifier fieldIdentifier)
88 {
89 ICheckRules model;
90
91 // Get access to the model via the required interface
92 model = fieldIdentifier.Model as ICheckRules;
93
94 // Check if the model was provided, and correctly cast
95 if (model == null)
96 {
97 throw new ArgumentException(
99 nameof(fieldIdentifier.Model), nameof(ICheckRules)));
100 }
101
102 // Transfer any broken rules of severity Error for the required property to the store
103 messages.Clear(fieldIdentifier);
104 foreach (BrokenRule brokenRule in model.GetBrokenRules())
105 {
106 if (brokenRule.Severity == RuleSeverity.Error)
107 {
108 if (fieldIdentifier.FieldName.Equals(brokenRule.Property))
109 {
110 // Add a message for each broken rule on the property under validation
111 messages.Add(fieldIdentifier, brokenRule.Description);
112 }
113 }
114 }
115
116 // We have to notify even if there were no messages before and are still no messages now,
117 // because the "state" that changed might be the completion of some async validation task
118 editContext.NotifyValidationStateChanged();
119 }
120 }
121}
A strongly-typed resource class, for looking up localized strings, etc.
static string InterfaceNotImplementedException
Looks up a localized string similar to {0} does not implement required interface {1}.
Stores details about a specific broken business rule.
Definition: BrokenRule.cs:19
string Property
Provides access to the property affected by the broken rule.
Definition: BrokenRule.cs:65
string Description
Provides access to the description of the broken rule.
Definition: BrokenRule.cs:55
RuleSeverity Severity
Gets the severity of the broken rule.
Definition: BrokenRule.cs:77
Tracks the business rules for a business object.
static BrokenRulesTree GetAllBrokenRules(object root)
Gets all nodes in tree that have IsValid = false (and all parents)
Defines the common methods for any business object which exposes means to supress and check business ...
Definition: ICheckRules.cs:18
BrokenRulesCollection GetBrokenRules()
Gets the broken rules collection
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16