A lightweight, flexible form generator for Blazor applications that creates dynamic forms from annotated C# classes.
- Creates forms automatically from C# classes
- Built-in validation through data annotations
- Customizable form rendering and layout
- Type-safe form handling
dotnet add package BlazorDynamicForm
- Add Service in Program.cs
builder.Services.AddBlazorDynamicForm();
- 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; }
}
- 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
}
}
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
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.