Skip to content

TheSmallPixel/BlazorDynamicForm

BlazorDynamicForm

A lightweight, flexible form generator for Blazor applications that creates dynamic forms from annotated C# classes.

Features

  • Creates forms automatically from C# classes
  • Built-in validation through data annotations
  • Customizable form rendering and layout
  • Type-safe form handling

Installation

dotnet add package BlazorDynamicForm 

Quick Start

  1. Add Service in Program.cs
builder.Services.AddBlazorDynamicForm();
  1. Create a Model with Annotations
public class ContactForm
{
    [Required, Display(Name = "Name")]
    public string Name { get; set; }
    
    [EmailAddress]
    public string Email { get; set; }
    
    [Phone, Display(Name = "Phone Number")]
    public string PhoneNumber { get; set; }
    
    [TextArea]
    public string Message { get; set; }
}
  1. Use the DynamicForm Component
@using BlazorDynamicForm.Components
@using TypeAnnotationParser

<DynamicForm Scheme="@_formScheme" Data="_formData" OnValidSubmit="@HandleSubmit">
    <SubmitTemplate>
        <button type="submit" class="btn btn-primary">Submit</button>
    </SubmitTemplate>
</DynamicForm>

@code {
    private SchemeModel _formScheme;
    private IDictionary<string, object> _formData = new Dictionary<string, object>();
    
    protected override void OnInitialized()
    {
        var parser = new TypeAnnotationParser();
        _formScheme = parser.Parse<ContactForm>();
    }
    
    private void HandleSubmit(IDictionary<string, object> data)
    {
        // Handle form data
    }
}
  1. Example

    Screenshot 2025-05-06 170325

Custom Attributes

BlazorDynamicForm provides additional attributes for enhanced form customization:

  • [TextArea] - Creates a multi-line text input
  • [Placeholder("Enter text...")] - Adds placeholder text
  • [Grid(6)] - Controls the layout grid width
  • [Name("Custom Field Name")] - Sets a custom field name
  • [MultipleSelect("Option1", "Option2", "Option3")] - Creates a dropdown with options

License

GPL-3.0

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Releases

No releases published

Packages

No packages published