Text Property

View Entity Text Property Configuration Options

1. Text Property

When you need to store text information in your application — like names, addresses, or descriptions — you should use a TextProperty. This type of property is designed specifically for text values and is one of the most commonly used property types in BlazorForKids. In the next example, we’ll show you how to define a simple text field and explain what each part does.

Let’s say you want to store a person’s first name. You can create a FirstName property using the generic method AddTextProperty<string>. This tells the framework that the value should be text. The property can also include configuration settings to control how it looks in forms, how it is validated (for example, making it required), and how it’s stored in the database. This makes it easy to manage everything related to that piece of information in one place.

publicpartialclassEmployee:IBkEntity<Employee,ApplicationDbContext>

{

    publicvoidBkConfiguration(IBkEntityDesigner<Employee,ApplicationDbContext>designer)

    {

// Note: The AddTextProperty method is generic, which means you must specify the data type it will use.// Note: The type can be either string or string?. Using string? tells the framework that the property is nullable.        designer.Properties.AddTextProperty<string>("FirstName",c=>

        {

// Sets the label displayed next to the editor.            c.Label("First Name");

// Sets the placeholder text shown inside the input field when it's empty.            c.PlaceHolder("Enter First Name");

// Adds a tooltip that appears when hovering over the input field.            c.ToolTip("Enter First Name");

// Sets the maximum number of characters allowed in the editor and in the database.            c.MaxLength(50);

// Enables browser-side validation by requiring a minimum number of characters.            c.MinLength(1);

// Displays the editor as a multi-line text area instead of a single-line input.            c.UseTextAreaEditor();

// Helps the browser determine what type of autocomplete suggestions to provide.            c.AutoComplete("given-name");

// Applies a custom CSS class to the editor for styling purposes.            c.CssClass("employee-first-name");

// Hides the editor by default in the form.// Useful when you have multiple forms and want this field hidden initially.// Note: visibility can also be controlled at the component level.            c.HideEditor();

// Hides the corresponding column in the grid view by default.// Useful when you have multiple grid views and want this column hidden initially.// Note: visibility can also be controlled at the component level.            c.HideGridViewColumn();

// Sets the tab order for the editor when navigating with the Tab key.            c.TabIndex(1);

// Applies a regular expression validation rule.// Only allows letters; shows an error if the rule is not satisfied.            c.RegularExpression("^[a-zA-Z]+$","First Name must contain only letters");

// Adds a custom validation rule based on the property value.// The first parameter is a lambda using the property's value.// The second is the error message shown if validation fails.// The third is the name of the generated custom validation attribute.            c.ValidationRuleValue(value=>value.Length>5,"First Name must be more than 5 characters","ValidateFirstNameLength");

// Adds a custom validation rule based on the entire model.// The lambda checks if both FirstName and LastName are longer than 5 characters.// The third parameter is the name of the generated custom validation attribute.            c.ValidationRuleModel(model=>model.FirstName.Length>5&&model.LastName.Length>5,"First Name and Last Name must be more than 5 characters","ValidateFirstNameAndLastNameLength");

// Adds a custom validation rule that uses the ApplicationDbContext.// Useful when checking database constraints, like uniqueness.// The rule must be synchronous (cannot use async/await).// In this example, it checks that the FirstName is unique in the database.            c.ValidationRuleUsingDbContext((dbContext,model)=>

            {

                varexists=dbContext.Employee.Where(a=>a.Id!=model.Id).Any(e=>e.FirstName==model.FirstName);

                return!exists;

            },"First Name already exists","ValidateFirstNameUnique");

// Applies an existing .NET validation attribute.// For example, EmailAddressAttribute can be used to validate an email format.            c.ValidationRuleUsingExistingAttribute(newEmailAddressAttribute());

        });

    }

}

Configuration Options for AddTextProperty<string>

Method Description Parameters
Label Sets the label text that appears next to the input editor. string label
PlaceHolder Sets the placeholder text shown inside the input field when it is empty. string placeholder
ToolTip Adds a tooltip that appears when hovering over the editor. string tooltip
MaxLength Sets the maximum number of characters allowed in the input field and the database column. int maxLength
MinLength Enables browser-side validation to require a minimum number of characters. int minLength
UseTextAreaEditor Changes the input from a single-line textbox to a multi-line textarea. None
AutoComplete Sets the autocomplete attribute to suggest a specific type of value (e.g., name, email). string autoCompleteValue
CssClass Applies a custom CSS class to the editor for styling purposes. string className
HideEditor Hides the editor from the edit form by default. None
HideGridViewColumn Hides the property column in the grid view by default. None
TabIndex Sets the tab order for the editor in the form. int tabIndex
RegularExpression Applies a regular expression validation rule to the property. string pattern, string errorMessage
ValidationRuleValue Defines a custom validation rule based on the property’s value. Func<T, bool> rule, string errorMessage, string attributeName
ValidationRuleModel Defines a custom validation rule using the entire model. Func<TModel, bool> rule, string errorMessage, string attributeName
ValidationRuleUsingDbContext Defines a custom validation rule that uses the database context and model. Useful for enforcing constraints like uniqueness. Func<DbContext, TModel, bool> rule, string errorMessage, string attributeName
ValidationRuleUsingExistingAttribute Applies an existing .NET validation attribute to the property. ValidationAttribute attribute
An unhandled error has occurred. Reload 🗙