Skip to content

Commit 4af78db

Browse files
[StaticFroms] Lots of work on localization.
1 parent 41c1c5c commit 4af78db

File tree

5 files changed

+126
-32
lines changed

5 files changed

+126
-32
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using DotNetElements.AppFramework.MudBlazorExtensions.Extensions;
2+
using Microsoft.AspNetCore.Components;
3+
using Microsoft.AspNetCore.Components.Forms;
4+
5+
namespace DotNetElements.AppFramework.MudBlazorExtensions.Components;
6+
7+
public sealed class DebugDataAnnotationsValidator : DataAnnotationsValidator
8+
{
9+
#if DEBUG
10+
[CascadingParameter]
11+
EditContext? DebugEditContext { get; set; }
12+
13+
protected override void OnInitialized()
14+
{
15+
base.OnInitialized();
16+
17+
if (DebugEditContext is null)
18+
{
19+
throw new InvalidOperationException($"{nameof(DebugDataAnnotationsValidator)} requires a cascading " +
20+
$"parameter of type {nameof(EditContext)}. Normally, you would use a {nameof(DebugDataAnnotationsValidator)} " +
21+
$"inside an EditForm.");
22+
}
23+
24+
DebugEditContext.OnValidationRequested += DebugEditContext_OnValidationRequested;
25+
}
26+
27+
protected override void Dispose(bool disposing)
28+
{
29+
if (disposing && DebugEditContext is not null)
30+
DebugEditContext.OnValidationRequested -= DebugEditContext_OnValidationRequested;
31+
}
32+
33+
private void DebugEditContext_OnValidationRequested(object? sender, ValidationRequestedEventArgs e)
34+
{
35+
DebugEditContext.LogDebugInfo(); // Debug only
36+
}
37+
#endif
38+
}

src/DotNetElements.AppFramework.MudBlazorExtensions/Components/EditScope.razor

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@using Microsoft.AspNetCore.Components.Forms
2+
23
@implements IDisposable
34

45
<CascadingValue Value="EditContext" IsFixed="true">
@@ -11,39 +12,42 @@
1112
[CascadingParameter]
1213
public required EditContext ParentEditContext { get; set; }
1314

14-
[Parameter]
15+
[Parameter, EditorRequired]
1516
public required RenderFragment ChildContent { get; set; }
1617

17-
[Parameter]
18+
[Parameter, EditorRequired]
1819
public required object Model { get; set; }
1920

20-
[Parameter] public bool Validate { get; set; } = true;
21+
[Parameter]
22+
public bool Validate { get; set; } = true;
23+
24+
[Parameter]
25+
public bool InvalidateParent { get; set; } = false;
26+
27+
public EditContext EditContext => editContext ??= new EditContext(Model);
2128

22-
private EditContext? _editContext;
23-
private ValidationMessageStore? _messageStore;
24-
private ValidationMessageStore? _parentMessageStore;
25-
public EditContext EditContext => _editContext ??= new EditContext(Model);
29+
private EditContext? editContext;
30+
private ValidationMessageStore? messageStore;
31+
private ValidationMessageStore? parentMessageStore;
2632

2733
protected override void OnInitialized()
2834
{
29-
base.OnInitialized();
35+
messageStore = new ValidationMessageStore(EditContext);
36+
parentMessageStore = new ValidationMessageStore(ParentEditContext);
37+
3038
ParentEditContext.OnValidationRequested += ParentEditContextOnOnValidationRequested;
31-
_messageStore = new ValidationMessageStore(EditContext);
32-
_parentMessageStore = new ValidationMessageStore(ParentEditContext);
3339
}
3440

3541
private void ParentEditContextOnOnValidationRequested(object? sender, ValidationRequestedEventArgs e)
3642
{
37-
_messageStore?.Clear();
38-
_parentMessageStore?.Clear();
43+
messageStore?.Clear();
44+
parentMessageStore?.Clear();
45+
3946
if (!Validate)
40-
{
4147
return;
42-
}
43-
if (!EditContext.Validate())
44-
{
45-
_parentMessageStore?.Add(() => Model, "Invalid");
46-
}
48+
49+
if (!EditContext.Validate() && InvalidateParent)
50+
parentMessageStore?.Add(() => Model, "Invalid");
4751
}
4852

4953
public void Dispose()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Components.Forms;
3+
4+
namespace DotNetElements.AppFramework.MudBlazorExtensions.Extensions;
5+
6+
public static class EditContextExtensions
7+
{
8+
[Conditional("DEBUG")]
9+
public static void LogDebugInfo(this EditContext? editContext)
10+
{
11+
if (editContext is null)
12+
return;
13+
14+
IEnumerable<string> messages = editContext.GetValidationMessages();
15+
16+
if (!messages.Any())
17+
return;
18+
19+
Console.WriteLine($"DEBUG EditContext validation messages. Context: {editContext.Model.GetType()}");
20+
21+
foreach (string message in messages)
22+
Console.WriteLine(message);
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace DotNetElements.Core.Extensions;
2+
3+
public static class DictionaryExtensions
4+
{
5+
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
6+
where TValue : new()
7+
{
8+
if (!dictionary.TryGetValue(key, out TValue? value))
9+
{
10+
value = new TValue();
11+
dictionary.Add(key, value);
12+
}
13+
14+
return value;
15+
}
16+
17+
public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
18+
{
19+
if (!dictionary.TryGetValue(key, out TValue? value))
20+
{
21+
dictionary.Add(key, defaultValue);
22+
return defaultValue;
23+
}
24+
25+
return value;
26+
}
27+
}

src/DotNetElements.Core/Validation/ValidateObject.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
44
public sealed class ValidateObjectAttribute : ValidationAttribute
55
{
6-
public ValidateObjectAttribute()
7-
: base("Nested item is not valid.")
8-
{
9-
}
6+
public bool ValidateAllProperties { get; set; } = true;
107

11-
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
12-
{
13-
if (value is null)
14-
return ValidationResult.Success;
8+
public ValidateObjectAttribute() : base("Nested item is not valid.")
9+
{
10+
}
1511

16-
ValidationContext context = new(value, validationContext, null);
17-
List<ValidationResult> results = [];
12+
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
13+
{
14+
if (value is null)
15+
return ValidationResult.Success;
1816

19-
if (!Validator.TryValidateObject(value, context, results, validateAllProperties: true))
20-
return new ValidationResult(ErrorMessage, validationContext.MemberName is not null ? [validationContext.MemberName] : null);
17+
ValidationContext context = new(value, validationContext, null);
18+
List<ValidationResult> results = [];
2119

22-
return ValidationResult.Success;
23-
}
20+
if (Validator.TryValidateObject(value, context, results, validateAllProperties: ValidateAllProperties))
21+
return ValidationResult.Success;
22+
23+
return new ValidationResult(ErrorMessage, validationContext.MemberName is not null ? [validationContext.MemberName] : null);
24+
}
2425
}

0 commit comments

Comments
 (0)