NvFieldcheckbox Component - Practical Guide for Blazor Developers
The NvFieldcheckbox component is a Blazor wrapper around Nova's checkbox component, providing seamless integration with Blazor's data binding, forms, and validation systems. It supports both individual checkboxes and checkbox groups.
General Blazor Concepts
For general information about data binding, forms, and validation in Nova Blazor components, see the Nova Blazor Integration Guide. This guide focuses on checkbox-specific features and patterns.
Basic Usage
Simple Checkbox
<NvFieldcheckbox Label="I agree to the terms and conditions" />
Checkbox with Description
<NvFieldcheckbox Label="Subscribe to newsletter"
Description="Receive weekly updates about our products and services" />
Data Binding
The component supports Blazor's @bind-Checked syntax for two-way data binding:
<NvFieldcheckbox @bind-Checked="isAgreed"
Label="I agree to the terms and conditions" />
@code {
private bool isAgreed = false;
}
For more information on data binding and event handling, see the Nova Blazor Integration Guide.
Label Placement
Label Position Options
<NvFieldcheckbox @bind-Checked="option1"
Label="Label after checkbox (default)"
LabelPlacement="@NvFieldcheckbox.LabelPlacementEnum.after" />
<NvFieldcheckbox @bind-Checked="option2"
Label="Label before checkbox"
LabelPlacement="@NvFieldcheckbox.LabelPlacementEnum.before" />
@code {
private bool option1 = false;
private bool option2 = false;
}
Form Integration and Validation
Checkboxes work seamlessly with Blazor's EditForm and validation. Use the Required attribute and For parameter for validation:
<EditForm Model="model" OnValidSubmit="Submit">
<DataAnnotationsValidator />
<NvFieldcheckbox @bind-Checked="model.TermsAccepted"
Label="I accept the terms and conditions"
Required="true"
For="@(() => model.TermsAccepted)" />
<button type="submit">Submit</button>
</EditForm>
@code {
private Model model = new();
public class Model
{
[Required(ErrorMessage = "You must accept the terms")]
public bool TermsAccepted { get; set; }
}
}
For detailed information on forms and validation, see the Nova Blazor Integration Guide.
Checkbox-Specific Patterns
Checkbox Groups with Arrays
<fieldset>
<legend>Select your hobbies:</legend>
<NvFieldcheckbox Checked="@IsHobbySelected("reading")"
Label="Reading"
CheckedChanged="@((isChecked) => OnHobbyChanged("reading", isChecked))" />
<NvFieldcheckbox Checked="@IsHobbySelected("sports")"
Label="Sports"
CheckedChanged="@((isChecked) => OnHobbyChanged("sports", isChecked))" />
<NvFieldcheckbox Checked="@IsHobbySelected("music")"
Label="Music"
CheckedChanged="@((isChecked) => OnHobbyChanged("music", isChecked))" />
<NvFieldcheckbox Checked="@IsHobbySelected("gaming")"
Label="Gaming"
CheckedChanged="@((isChecked) => OnHobbyChanged("gaming", isChecked))" />
</fieldset>
@if (selectedHobbies.Any())
{
<div class="selected-hobbies">
<h4>Selected hobbies:</h4>
<ul>
@foreach (var hobby in selectedHobbies)
{
<li>@hobby</li>
}
</ul>
</div>
}
@code {
private List<string> selectedHobbies = new();
private void OnHobbyChanged(string hobby, bool isChecked)
{
if (isChecked)
{
if (!selectedHobbies.Contains(hobby))
selectedHobbies.Add(hobby);
}
else
{
selectedHobbies.Remove(hobby);
}
}
private bool IsHobbySelected(string hobby) => selectedHobbies.Contains(hobby);
}
Indeterminate State for Hierarchical Checkboxes
<NvFieldcheckbox Checked="@selectAll"
Indeterminate="@isIndeterminate"
Label="Select All Notifications"
CheckedChanged="OnSelectAllChanged" />
<div class="indented-options">
@foreach (var child in notifications)
{
<NvFieldcheckbox Checked="@child.Checked"
Label="@child.Label"
CheckedChanged="@(value => OnChildClicked(child, value))" />
}
</div>
@code {
private bool selectAll;
private bool isIndeterminate;
// Flag to ignore child events during programmatic updates
private bool suppressChildEvents = false;
private List<NotificationItem> notifications = new()
{
new NotificationItem("Email notifications"),
new NotificationItem("SMS notifications"),
new NotificationItem("Push notifications")
};
/// <summary>
/// Handles clicks on the "Select All" checkbox
/// </summary>
private void OnSelectAllChanged(bool isChecked)
{
Console.WriteLine($"[SelectAll] Clicked -> {isChecked}");
// Suppress child events while updating all children
suppressChildEvents = true;
foreach (var child in notifications)
{
child.Checked = isChecked;
Console.WriteLine($" [Child] {child.Label} set to {child.Checked}");
}
selectAll = isChecked;
isIndeterminate = false;
suppressChildEvents = false;
Console.WriteLine($"[SelectAll] State -> selectAll={selectAll}, isIndeterminate={isIndeterminate}");
}
/// <summary>
/// Handles clicks on individual child checkboxes
/// </summary>
private void OnChildClicked(NotificationItem child, bool newValue)
{
if (suppressChildEvents)
{
Console.WriteLine($"[Child] Ignored {child.Label} event during bulk update");
return;
}
// Update the clicked child only
child.Checked = newValue;
Console.WriteLine($"[Child] {child.Label} Clicked -> {child.Checked}");
// Recompute Select All / Indeterminate state
var allChecked = notifications.All(n => n.Checked);
var noneChecked = notifications.All(n => !n.Checked);
selectAll = allChecked;
isIndeterminate = !allChecked && !noneChecked;
Console.WriteLine($"[State] selectAll={selectAll}, isIndeterminate={isIndeterminate}");
}
/// <summary>
/// Represents a single notification item
/// </summary>
private class NotificationItem
{
public string Label { get; }
public bool Checked { get; set; }
public NotificationItem(string label)
{
Label = label;
Checked = false;
}
}
}
Best Practices
- Use clear, descriptive labels that clearly indicate what the checkbox controls
- Provide descriptions for complex options to help users understand the implications
- Group related checkboxes using fieldsets and legends for better accessibility
- Use indeterminate state appropriately for hierarchical selections (parent-child relationships)
- Consider master/slave relationships for "Select All" functionality
- Test keyboard navigation and screen reader compatibility
- Use appropriate label placement (
beforeorafter) based on your design requirements
For general Blazor patterns, forms, validation, and more examples, see the Nova Blazor Integration Guide.