Blazor playground

Nova v3.37.0

Preview

Blazor

Fieldtext

← nv-fieldtext

NvFieldtext Component - Practical Guide for Blazor Developers

The NvFieldtext component is a Blazor wrapper around Nova's text input component, providing seamless integration with Blazor's data binding, forms, and validation systems.

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 text input-specific features and patterns.

Basic Usage

Simple Text Input

razor<NvFieldtext Label="First Name" Placeholder="Enter your first name" />

Text Input with Description

razor<NvFieldtext
    Label="Email Address"
    Description="We'll use this to send you important updates"
    Placeholder="your.email@example.com" />

Data Binding

The component supports Blazor's @bind-Value syntax for two-way data binding:

razor<NvFieldtext @bind-Value="firstName"
             Label="First Name"
             Placeholder="Enter your first name" />

@code {
    private string firstName = "";
}

For more information on data binding and event handling, see the Nova Blazor Integration Guide.

Input Types

The component supports different input types for better user experience and validation:

Email Input

razor<NvFieldtext
    @bind-Value="email"
    Label="Email Address"
    Type="TypeEnum.email"
    Placeholder="your.email@example.com" />

Phone Input

razor<NvFieldtext
    @bind-Value="phone"
    Label="Phone Number"
    Type="TypeEnum.tel"
    Placeholder="+1 (555) 123-4567" />

Text Input (Default)

razor<NvFieldtext
    @bind-Value="username"
    Label="Username"
    Placeholder="Choose a username" />

Form Integration

The component integrates seamlessly with Blazor's EditForm for validation and form submission:

Basic Form Integration

razor<EditForm Model="userModel" OnValidSubmit="HandleValidSubmit">
    <DataAnnotationsValidator />

    <NvFieldtext
        @bind-Value="userModel.FirstName"
        Label="First Name"
        For="() => userModel.FirstName" />

    <NvFieldtext
        @bind-Value="userModel.LastName"
        Label="Last Name"
        For="() => userModel.LastName" />

    <NvFieldtext
        @bind-Value="userModel.Email"
        Label="Email"
        Type="TypeEnum.email"
        For="() => userModel.Email" />

    <button type="submit">Register</button>
</EditForm>

@code {
    private UserModel userModel = new();

    private void HandleValidSubmit()
    {
        // Process form submission
        Console.WriteLine("User registered successfully!");
    }

    public class UserModel
    {
        [Required(ErrorMessage = "First name is required")]
        public string? FirstName { get; set; }

        [Required(ErrorMessage = "Last name is required")]
        public string? LastName { get; set; }

        [Required(ErrorMessage = "Email is required")]
        [EmailAddress(ErrorMessage = "Please enter a valid email address")]
        public string? Email { get; set; }
    }
}

Validation

Required Field Validation

razor<EditForm Model="contactModel" OnValidSubmit="HandleSubmit">
    <DataAnnotationsValidator />

    <NvFieldtext
        @bind-Value="contactModel.Name"
        Label="Full Name"
        Required="true"
        For="() => contactModel.Name" />

    <NvFieldtext
        @bind-Value="contactModel.Phone"
        Label="Phone Number"
        Type="TypeEnum.tel"
        For="() => contactModel.Phone" />

    <button type="submit">Submit</button>
</EditForm>

@code {
    private ContactModel contactModel = new();

    public class ContactModel
    {
        [Required(ErrorMessage = "Name is required")]
        public string? Name { get; set; }

        [Phone(ErrorMessage = "Please enter a valid phone number")]
        public string? Phone { get; set; }
    }
}

Length Validation

razor<EditForm Model="accountModel" OnValidSubmit="HandleSubmit">
    <DataAnnotationsValidator />

    <NvFieldtext
        @bind-Value="accountModel.Username"
        Label="Username"
        Minlength="3"
        Maxlength="20"
        For="() => accountModel.Username" />

    <NvFieldtext
        @bind-Value="accountModel.Bio"
        Label="Bio"
        Maxlength="150"
        Description="Tell us about yourself (max 150 characters)"
        For="() => accountModel.Bio" />

    <button type="submit">Save Profile</button>
</EditForm>

@code {
    private AccountModel accountModel = new();

    public class AccountModel
    {
        [Required]
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Username must be between 3 and 20 characters")]
        public string? Username { get; set; }

        [StringLength(150, ErrorMessage = "Bio cannot exceed 150 characters")]
        public string? Bio { get; set; }
    }
}

Pattern Validation

razor<EditForm Model="productModel" OnValidSubmit="HandleSubmit">
    <DataAnnotationsValidator />

    <NvFieldtext
        @bind-Value="productModel.ProductCode"
        Label="Product Code"
        Pattern="[A-Z]{2}[0-9]{4}"
        Description="Format: AA1234 (2 letters followed by 4 numbers)"
        For="() => productModel.ProductCode" />

    <NvFieldtext
        @bind-Value="productModel.ZipCode"
        Label="ZIP Code"
        Pattern="[0-9]{5}"
        Placeholder="12345"
        For="() => productModel.ZipCode" />

    <button type="submit">Add Product</button>
</EditForm>

@code {
    private ProductModel productModel = new();

    public class ProductModel
    {
        [Required]
        [RegularExpression("[A-Z]{2}[0-9]{4}", ErrorMessage = "Product code must be 2 letters followed by 4 numbers")]
        public string? ProductCode { get; set; }

        [Required]
        [RegularExpression("[0-9]{5}", ErrorMessage = "ZIP code must be 5 digits")]
        public string? ZipCode { get; set; }
    }
}

Text Input-Specific Patterns

Input Types

Use appropriate input types for better UX and native validation:

razor<NvFieldtext @bind-Value="email"
             Label="Email"
             Type="TypeEnum.email" />

<NvFieldtext @bind-Value="phone"
             Label="Phone"
             Type="TypeEnum.tel" />

@code {
    private string email = "";
    private string phone = "";
}

Length Constraints

razor<NvFieldtext @bind-Value="username"
             Label="Username"
             Minlength="3"
             Maxlength="20"
             Description="3-20 characters" />

@code {
    private string username = "";
}

Pattern Validation

razor<NvFieldtext @bind-Value="zipCode"
             Label="ZIP Code"
             Pattern="[0-9]{5}"
             Placeholder="12345" />

@code {
    private string zipCode = "";
}

Real-time Validation

razor<NvFieldtext
    @bind-Value="username"
    Label="Username"
    ValueChanged="ValidateUsername"
    Description="@validationMessage" />

@code {
    private string username = "";
    private string validationMessage = "";

    private void ValidateUsername(string value)
    {
        username = value;

        if (string.IsNullOrWhiteSpace(value))
        {
            validationMessage = "";
        }
        else if (value.Length < 3)
        {
            validationMessage = "Username must be at least 3 characters";
        }
        else if (value.Contains(" "))
        {
            validationMessage = "Username cannot contain spaces";
        }
        else
        {
            validationMessage = "✓ Username available";
        }
    }
}

Conditional Fields

razor<NvFieldtext
    @bind-Value="contactMethod"
    Label="Preferred Contact Method"
    Placeholder="email, phone, etc." />

@if (contactMethod?.ToLower() == "email")
{
    <NvFieldtext
        @bind-Value="email"
        Label="Email Address"
        Type="TypeEnum.email"
        Required="true" />
}
else if (contactMethod?.ToLower() == "phone")
{
    <NvFieldtext
        @bind-Value="phone"
        Label="Phone Number"
        Type="TypeEnum.tel"
        Required="true" />
}

@code {
    private string contactMethod = "";
    private string email = "";
    private string phone = "";
}

Search with Debouncing

razor<NvFieldtext
    @bind-Value="searchQuery"
    Label="Search Products"
    Placeholder="Type to search..."
    ValueChanged="OnSearchChanged" />

@if (isSearching)
{
    <p>Searching...</p>
}
else if (searchResults.Any())
{
    <ul>
        @foreach (var result in searchResults)
        {
            <li>@result</li>
        }
    </ul>
}

@code {
    private string searchQuery = "";
    private List<string> searchResults = new();
    private bool isSearching = false;
    private CancellationTokenSource? _debounceCts;

    private async Task OnSearchChanged(string value)
    {
        searchQuery = value;

        // Cancel previous search
        _debounceCts?.Cancel();
        _debounceCts = new CancellationTokenSource();

        try
        {
            await Task.Delay(300, _debounceCts.Token); // 300ms debounce

            if (value.Length >= 2)
            {
                isSearching = true;
                await PerformSearch(value);
            }
            else
            {
                searchResults.Clear();
            }
        }
        catch (TaskCanceledException)
        {
            // Search was cancelled, ignore
        }
        finally
        {
            isSearching = false;
        }
    }

    private async Task PerformSearch(string query)
    {
        // Simulate API call
        await Task.Delay(500);
        searchResults = new List<string> { $"{query} result 1", $"{query} result 2", $"{query} result 3" };
    }
}

Best Practices

  1. Use appropriate input types (email, tel, text) for better UX and native validation
  2. Combine Minlength/Maxlength with Pattern for comprehensive client-side validation
  3. Use placeholders to show expected format examples
  4. Consider debouncing for search inputs to avoid excessive API calls
  5. Provide real-time feedback for username availability or format validation

For general Blazor patterns, forms, validation, and more examples, see the Nova Blazor Integration Guide.