public class Employee { [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } }
Blazorise DataGrid: Validations
Easily validate the edited or added row data.
Overview
The DataGrid
provides validations of column values at editing or creating items. For using validation of DataGrid
you have to use these properties:
UseValidation
must be set to true to enable validation.ShowValidationFeedback
of DataGrid to hide or show feedback for validation.ShowValidationsSummary
of DataGrid to hide or show validations summary.ValidationsSummaryLabel
of DataGrid to set label of validations summary.Validator
ofDataGridColumn
validates the input value after trying to save.ValidationPattern
ofDataGridColumn
forces validation to use regex pattern matching instead of default validator handler.
Examples
Validations
By default,DataGrid
will use data-annotation to validate editing fields. You only need to define them on your model and they will automatically be picked up by the grid.
Note: Don’t forget to enable validation by setting
UseValidation
.
To override data-annotation you only need to define a
Validator
attribute and assign it to your validation method.
<DataGrid TItem="Employee" Data="" Responsive Editable UseValidation> <DataGridColumn Field="@nameof( Employee.FirstName )" Caption="Name" Validator="" Editable /> <DataGridCommandColumn /> </DataGrid>
@code{ private List<Employee> employeeList = new() { new() { FirstName = "David" }, new() { FirstName = "Mladen" }, new() { FirstName = "John" }, new() { FirstName = "Ana" }, new() { FirstName = "Jessica" } }; public void CheckName( ValidatorEventArgs validationArgs ) { ValidationRule.IsNotEmpty( validationArgs ); if ( validationArgs.Status == ValidationStatus.Error ) { validationArgs.ErrorText = "Name can't be empty."; } } }
If you use
EditTemplate
to customize editing of columns, then using Validator
or ValidationPattern
will not work and you have to use Validation
like in the following example:
<DataGrid TItem="Employee" Data="" Responsive Editable UseValidation ShowValidationsSummary="false"> <DataGridColumn Field="@nameof( Employee.FirstName )" Caption="Name" Editable> <EditTemplate> <Validation Validator=""> <TextEdit Text="@((string)context.CellValue)" TextChanged="(value => context.CellValue = value)"> <Feedback> <ValidationError /> </Feedback> </TextEdit> </Validation> </EditTemplate> </DataGridColumn> <DataGridCommandColumn /> </DataGrid>
@code{ private List<Employee> employeeList = new() { new() { FirstName = "David" }, new() { FirstName = "Mladen" }, new() { FirstName = "John" }, new() { FirstName = "Ana" }, new() { FirstName = "Jessica" } }; public void CheckName( ValidatorEventArgs validationArgs ) { ValidationRule.IsNotEmpty( validationArgs ); if ( validationArgs.Status == ValidationStatus.Error ) { validationArgs.ErrorText = "Name can't be empty."; } } }
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.