-
Notifications
You must be signed in to change notification settings - Fork 469
Add FluentValidationMessage #4472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev-v5
Are you sure you want to change the base?
Changes from all commits
abda901
9c2f91b
749dce9
9de8d1f
3d7774b
6c375cb
7621ff5
d8bb6f9
899a917
fe4beab
84b2f80
ebc5788
9548274
7c5181c
802b699
05223c5
9c330df
dbc18e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| description: 'Blazor component and application patterns' | ||
| applyTo: '**/*.razor, **/*.razor.cs, **/*.razor.css' | ||
| --- | ||
|
|
||
| ## Blazor Code Style and Structure | ||
|
|
||
| - Write idiomatic and efficient Blazor and C# code. | ||
| - Follow .NET and Blazor conventions. | ||
| - Use Razor Components appropriately for component-based UI development. | ||
| - Prefer inline functions for smaller components but separate complex logic into code-behind or service classes. | ||
| - Async/await should be used where applicable to ensure non-blocking UI operations. | ||
|
|
||
| ## Naming Conventions | ||
|
|
||
| - Follow PascalCase for component names, method names, and public members. | ||
| - Use camelCase for private fields and local variables. | ||
| - Prefix interface names with "I" (e.g., IUserService). | ||
|
|
||
| ## Blazor and .NET Specific Guidelines | ||
|
|
||
| - Utilize Blazor's built-in features for component lifecycle (e.g., OnInitializedAsync, OnParametersSetAsync). | ||
| - Use data binding effectively with @bind. | ||
| - Leverage Dependency Injection for services in Blazor. | ||
| - Structure Blazor components and services following Separation of Concerns. | ||
| - Always use the latest version C#, currently C# 13 features like record types, pattern matching, and global usings. | ||
|
|
||
| ## Error Handling and Validation | ||
|
|
||
| - Implement proper error handling for Blazor pages and API calls. | ||
| - Use logging for error tracking in the backend and consider capturing UI-level errors in Blazor with tools like ErrorBoundary. | ||
| - Implement validation using FluentValidation or DataAnnotations in forms. | ||
|
|
||
| ## Blazor API and Performance Optimization | ||
|
|
||
| - Utilize Blazor server-side or WebAssembly optimally based on the project requirements. | ||
| - Use asynchronous methods (async/await) for API calls or UI actions that could block the main thread. | ||
| - Optimize Razor components by reducing unnecessary renders and using StateHasChanged() efficiently. | ||
| - Minimize the component render tree by avoiding re-renders unless necessary, using ShouldRender() where appropriate. | ||
| - Use EventCallbacks for handling user interactions efficiently, passing only minimal data when triggering events. | ||
|
|
||
| ## Caching Strategies | ||
|
|
||
| - Implement in-memory caching for frequently used data, especially for Blazor Server apps. Use IMemoryCache for lightweight caching solutions. | ||
| - For Blazor WebAssembly, utilize localStorage or sessionStorage to cache application state between user sessions. | ||
| - Consider Distributed Cache strategies (like Redis or SQL Server Cache) for larger applications that need shared state across multiple users or clients. | ||
| - Cache API calls by storing responses to avoid redundant calls when data is unlikely to change, thus improving the user experience. | ||
|
|
||
| ## State Management Libraries | ||
|
|
||
| - Use Blazor's built-in Cascading Parameters and EventCallbacks for basic state sharing across components. | ||
| - Implement advanced state management solutions using libraries like Fluxor or BlazorState when the application grows in complexity. | ||
| - For client-side state persistence in Blazor WebAssembly, consider using Blazored.LocalStorage or Blazored.SessionStorage to maintain state between page reloads. | ||
| - For server-side Blazor, use Scoped Services and the StateContainer pattern to manage state within user sessions while minimizing re-renders. | ||
|
|
||
| ## API Design and Integration | ||
|
|
||
| - Use HttpClient or other appropriate services to communicate with external APIs or your own backend. | ||
| - Implement error handling for API calls using try-catch and provide proper user feedback in the UI. | ||
|
|
||
| ## Testing and Debugging in Visual Studio | ||
|
|
||
| - All unit testing and integration testing should be done in Visual Studio Enterprise. | ||
| - Test Blazor components and services using bUnit and xUnit. | ||
| - Debug Blazor UI issues using browser developer tools and Visual Studio's debugging tools for backend and server-side issues. | ||
| - For performance profiling and optimization, rely on Visual Studio's diagnostics tools. | ||
| - Do not use grep to search file contents. Use the Select-String PowerShell cmdlet or Visual Studio's built-in search functionality. | ||
|
|
||
| ## Security and Authentication | ||
|
|
||
| - Implement Authentication and Authorization in the Blazor app where necessary using ASP.NET Identity or JWT tokens for API authentication. | ||
| - Use HTTPS for all web communication and ensure proper CORS policies are implemented. | ||
|
|
||
| ## API Documentation and Swagger | ||
|
|
||
| - Use Swagger/OpenAPI for API documentation for your backend API services. | ||
| - Ensure XML documentation for models and API methods for enhancing Swagger documentation. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,46 @@ | ||
| <FluentTextInput Label="Error state" | ||
| MessageState="@MessageState.Error" | ||
| Message="This is an error message." | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <FluentTextInput Label="Warning state" | ||
| MessageState="@MessageState.Warning" | ||
| Message="This is a warning message." | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <FluentTextInput Label="Success state" | ||
| MessageState="@MessageState.Success" | ||
| Message="This is a success message." | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <FluentTextInput Label="Custom state" | ||
| Message="This is a custom message." | ||
| MessageIcon="@(new Icons.Regular.Size24.Accessibility())" | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
| <EditForm Model="@model" OnValidSubmit="@HandleValidSubmit" FormName="entry_form" novalidate="true"> | ||
|
|
||
| <FluentTextInput Label="Error state" | ||
| @bind-Value="model.Text" | ||
| MessageState="@MessageState.Error" | ||
| Message="This is an error message." | ||
| MessageCondition="@FluentFieldCondition.Always" | ||
| Required="true" /> | ||
|
|
||
|
|
||
| <FluentTextInput Label="Warning state" | ||
| MessageState="@MessageState.Warning" | ||
| Message="This is a warning message." | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <FluentTextInput Label="Success state" | ||
| MessageState="@MessageState.Success" | ||
| Message="This is a success message." | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <FluentTextInput Label="Custom state" | ||
| Message="This is a custom message." | ||
| MessageIcon="@(new Icons.Regular.Size24.Accessibility())" | ||
| MessageCondition="@FluentFieldCondition.Always" /> | ||
|
|
||
| <div> | ||
| <FluentButton Type="ButtonType.Submit" Appearance="ButtonAppearance.Primary">Submit</FluentButton> | ||
| </div> | ||
| </EditForm> | ||
|
|
||
| @code { | ||
| [SupplyParameterFromForm] | ||
| private Model model { get; set; } = new(); | ||
|
|
||
| private void HandleValidSubmit() | ||
| { | ||
| Console.WriteLine("HandleValidSubmit called"); | ||
|
|
||
| // Process the valid form | ||
| } | ||
|
|
||
| private class Model | ||
| { | ||
| public string? Text { get; set; } = string.Empty; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| form div:not(.fluent-stack-vertical) { | ||
| display: inline-flex; | ||
| margin-bottom: 1rem; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,14 @@ public class Starship | |||||
| [StringLength(16, ErrorMessage = "Identifier too long (16 character limit)")] | ||||||
| public string? Identifier { get; set; } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// The unique identifier for the starship. | ||||||
|
||||||
| /// The unique identifier for the starship. | |
| /// The password used to secure access to the starship. |
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RegularExpression pattern "[A-Z][a-z]{\d}+" is not a valid regex quantifier ({\d} will throw at runtime when validation runs). If the intent is to require at least one uppercase, one lowercase, and one digit, update the pattern accordingly (e.g., using lookaheads or a corrected quantifier).
| [RegularExpression(@"[A-Z][a-z]{\d}+", ErrorMessage = "Password must contain at least one uppercase letter, one lowercase letter and one digit")] | |
| [RegularExpression(@"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$", ErrorMessage = "Password must contain at least one uppercase letter, one lowercase letter and one digit")] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,23 +76,20 @@ public bool Build(FluentFieldConditionOptions? options = null) | |
| { | ||
| if (item.Condition.Invoke()) | ||
| { | ||
| var coloredMessage = FluentFieldParameterSelector.StateToMessageTemplate(item.State, item.Message); | ||
|
|
||
| messages.Add(builder => | ||
| { | ||
| builder.OpenElement(0, "div"); | ||
| builder.AddContent(1, FluentField.CreateIcon(item.Icon ?? FluentFieldParameterSelector.StateToIcon(item.State))); | ||
|
|
||
| if (item.State is null) | ||
| { | ||
| builder.AddContent(2, item.Message); | ||
| } | ||
| else | ||
| builder.OpenComponent<FluentText>(0); | ||
| builder.AddComponentParameter(1, "As", TextTag.Span); | ||
| builder.AddComponentParameter(2, "Color", item.State == MessageState.Error ? Color.Error : Color.Info); | ||
| //builder.AddAttribute(4, "slot", "message"); | ||
| builder.AddAttribute(5, "style", "display: flex; align-items: center;"); | ||
| builder.AddAttribute(6, "ChildContent", (RenderFragment)(contentBuilder => | ||
| { | ||
| builder.AddContent(2, coloredMessage); | ||
| } | ||
| contentBuilder.AddContent(0, FluentField.CreateIcon(item.Icon ?? FluentFieldParameterCollector.StateToIcon(item.State))); | ||
| contentBuilder.AddContent(1, item.Message); | ||
| })); | ||
|
Comment on lines
+81
to
+90
|
||
|
|
||
| builder.CloseElement(); | ||
| builder.CloseComponent(); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example text says the form uses
FluentValidationMessages, but allFluentValidationMessageusages in the markup are currently commented out. Either uncomment at least one instance to demonstrate it, or adjust the description so it matches what the page actually renders.