CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
CslaValidationMessages.razor.cs
Go to the documentation of this file.
1using Csla.Core;
2using Csla.Rules;
3using Microsoft.AspNetCore.Components;
4using Microsoft.AspNetCore.Components.Forms;
5using System;
6using System.Collections.Generic;
7using System.Linq;
8using System.Linq.Expressions;
9using System.Threading.Tasks;
10
11namespace Csla.Blazor
12{
16 public class CslaValidationMessageBase<PropertyType> : ComponentBase, IDisposable
17 {
18
22 protected bool _validationInitiated = false;
23 private FieldIdentifier _fieldIdentifier;
24 private EditContext _previousEditContext;
25 private EventHandler<FieldChangedEventArgs> _fieldChangedHandler;
26 private EventHandler<ValidationStateChangedEventArgs> _validationStateChangedHandler;
27
31 [Parameter] public Expression<Func<PropertyType>> For { get; set; }
35 [Parameter] public string WrapperId { get; set; } = "wrapper";
39 [Parameter] public string WrapperClass { get; set; } = "validation-messages";
43 [Parameter] public string ErrorClass { get; set; } = "text-danger";
47 [Parameter] public string WarningClass { get; set; } = "text-warning";
51 [Parameter] public string InfoClass { get; set; } = "text-info";
55 [Parameter] public string ErrorWrapperClass { get; set; } = "error-messages";
59 [Parameter] public string WarningWrapperClass { get; set; } = "warning-messages";
63 [Parameter] public string InfoWrapperClass { get; set; } = "information-messages";
67 [CascadingParameter] protected EditContext CurrentEditContext { get; set; }
68
69 #region Event Handlers
70
74 protected override void OnInitialized()
75 {
76 // Initialise event handler delegates for use in capturing state changes
77 _fieldChangedHandler = (sender, eventArgs) => OnFieldChanged(sender, eventArgs);
78 _validationStateChangedHandler = (sender, eventArgs) => OnValidationStateChanged(sender, eventArgs);
79 }
80
84 protected override void OnParametersSet()
85 {
86 // Check that the required parameters have been provided
87 if (CurrentEditContext == null)
88 {
89 throw new InvalidOperationException(
91 nameof(CslaValidationMessages<string>), nameof(EditContext)));
92
93 }
94
95 if (For == null)
96 throw new ArgumentNullException(nameof(For));
97
98 // Create a FieldIdentifier to use in recognising the field being validated
99 _fieldIdentifier = FieldIdentifier.Create(For);
100
101 // Wire up handling of the state change events we need event
102 if (CurrentEditContext != _previousEditContext)
103 {
105 CurrentEditContext.OnFieldChanged += _fieldChangedHandler;
106 CurrentEditContext.OnValidationStateChanged += _validationStateChangedHandler;
107 _previousEditContext = CurrentEditContext;
108 }
109 }
110
116 protected void OnFieldChanged(object sender, FieldChangedEventArgs eventArgs)
117 {
118 if (eventArgs.FieldIdentifier.Equals(_fieldIdentifier))
119 {
121 StateHasChanged();
122 }
123 }
124
130 protected void OnValidationStateChanged(object sender, ValidationStateChangedEventArgs eventArgs)
131 {
132 IEnumerable<string> messages;
133
134 // Retrieve the messages for the property in which we are interested
135 messages = CurrentEditContext.GetValidationMessages(_fieldIdentifier);
136
137 if (messages.Any())
138 {
139 // If any messages are present then validation has been run
140 // This is a cheat used to identify that the form has been submitted with invalid data
142 }
143
144 StateHasChanged();
145 }
146
151 {
152 if (_previousEditContext != null)
153 {
154 // Unhook any event handler made to a different edit context
155 _previousEditContext.OnFieldChanged -= _fieldChangedHandler;
156 _previousEditContext.OnValidationStateChanged -= _validationStateChangedHandler;
157 }
158 }
159
160 #endregion
161
162 #region Message Retrieval
163
168 protected IEnumerable<string> GetErrorMessages()
169 {
170 return GetBrokenRuleMessages(RuleSeverity.Error);
171 }
172
177 protected IEnumerable<string> GetWarningMessages()
178 {
179 return GetBrokenRuleMessages(RuleSeverity.Warning);
180 }
181
186 protected IEnumerable<string> GetInfoMessages()
187 {
188 return GetBrokenRuleMessages(RuleSeverity.Information);
189 }
190
191 private IEnumerable<string> GetBrokenRuleMessages(RuleSeverity severity)
192 {
193 IList<string> messages = new List<string>();
194 ICheckRules objectUnderTest;
195
196 // Attempt to gain access to the underlying CSLA object
197 objectUnderTest = _fieldIdentifier.Model as ICheckRules;
198 if (objectUnderTest == null)
199 {
200 throw new ArgumentException(nameof(_fieldIdentifier.Model));
201 }
202
203 // Iterate through the broken rules to find the subset we want
204 foreach (BrokenRule rule in objectUnderTest.GetBrokenRules())
205 {
206 // Exclude any broken rules that are not for the property we are interested in
207 if (rule.Property.Equals(_fieldIdentifier.FieldName, StringComparison.InvariantCultureIgnoreCase))
208 {
209 // Exclude any of a severity other than that we want
210 if (rule.Severity == severity)
211 {
212 // Rule meets our criteria, so add its text to the list we are to return
213 messages.Add(rule.Description);
214 }
215 }
216 }
217
218 // Return the list of messages that matched the criteria
219 return messages;
220 }
221
222 #endregion
223
224 #region IDisposable Interface
225
229 public void Dispose()
230 {
231 Dispose(true);
232 }
233
238 protected virtual void Dispose(bool disposing)
239 {
240 if (disposing)
242 }
243
244 #endregion
245
246 }
247}
IEnumerable< string > GetInfoMessages()
Get info messages
bool _validationInitiated
Value indicating whether validation is initiated
string InfoWrapperClass
Gets or sets the info wrapper class
string WarningWrapperClass
Gets or sets the warning wrapper class
string InfoClass
Gets or sets the info class
void DetachPreviousEventHandlers()
Detatch previous event handlers
Expression< Func< PropertyType > > For
Gets or sets the expression t use during validation
void OnValidationStateChanged(object sender, ValidationStateChangedEventArgs eventArgs)
On validation state changed method
override void OnInitialized()
On initialized method
string WrapperClass
Gets or sets the wrapper class
IEnumerable< string > GetErrorMessages()
Get error messages
override void OnParametersSet()
On parameters set method
void OnFieldChanged(object sender, FieldChangedEventArgs eventArgs)
On field changed method
EditContext CurrentEditContext
Gets or sets the current edit context
string ErrorClass
Gets or sets the error class
string WrapperId
Gets or sets the wrapper id
virtual void Dispose(bool disposing)
Dispose the object
string ErrorWrapperClass
Gets or sets the error wrapper class
IEnumerable< string > GetWarningMessages()
Get warning messages
string WarningClass
Gets or sets the warning class
A strongly-typed resource class, for looking up localized strings, etc.
static string CascadingEditContextRequiredException
Looks up a localized string similar to {0} requires a cascading parameter of type {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
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