ASP.NET Core FlexGrid - Validation Using the Error Tip
Data validation is an important feature used to allow only specific data entries. This is useful in situations when end-users have the permissions to add/edit data. For example, we may want to limit the data entry into cells to whole numbers, specific text, or to confirm to a regular expression pattern (like an email address).
In the ComponentOne v2 release, we added the ErrorTip for FlexGrid, to display validation error labels based on specific validation rules for columns. ErrorTip can also be customized to validate edits, format tooltip content using CSS.
The complete API for this provider is available here.
In this article, we'll discuss an example where we can add different validation rules using the Error ToolTip in FlexGrid.
Using ErrorTip for FlexGrid with Inventory Management
Let’s take an example of a furniture store that needs to manage its inventory. The store manager is tasked with the job of updating the inventory records after each sale and the fields have certain validation rules.
The Inventory table consists of the following fields and validation rules:
Field Name |
Description | Validation Rule |
Model | Model number of Item | Model number should start with Prefix S-S |
Units | Number of remaining units in Inventory | Number of units sold cannot be less than 1 or more than total inventory |
Cost Price | Purchase price of item | Read only |
Sale Price | Sale price of item | Should not be less than cost price |
Units in Stock | No of units available | Read only |
Customer Name | Customer name | Cannot be empty/null |
Customer Email | Email of customer | Should be a valid Email address |
Customer Phone | Customer’s contact conformation | Should be at least 7 digits |
These rules may seem to be difficult for the store manager to remember while entering records. It would make the store manager’s job easier if there was a way to display messages when these validation rules are not satisfied.
FlexGrid & ErrorTip
Using the FlexGrid & ErrorTip we can easily solve the store manager’s problem. FlexGrid will be used to display and edit inventory records and the Error Tip will be used to implement client-side validation rules for each of these fields.
- Initializing FlexGrid with data & adding columns
- Adding validation rules using ErrorTip
- Formatting the appearance using CSS
Initializing FlexGrid with Data & Adding Columns
Let’s initialize FlexGrid with data form the inventory table and add the field names as columns
Index.cshtml
Adding Validation Rules Using ErrorTip
In order to implement validation rules, we'll bind the FlexGrid’s fields to the javascript function ValidateGrid() using the collection view’s GetError() method. The ValidateGrid() contains the logic which implements the validation rules for columns.
Now, we'll take a look into the Javascript ValidateGrid() function which implements the validation rules for the fields. This function accepts item and prop as arguments, they are used to identify fields and apply corresponding validation rules.
Let’s look at the validations of each of the fields and their implementations inside the ValidateGrid function (). ToolTip Messages also support HTML content and the error messages can be formatted with HTML elements.
Formatting the Appearance Using CSS
We can also format the appearance of the ToolTip using CSS and customize CSS properties like height, padding, text-alignment, border etc.
Here’s how the ErrorTip validation looks:
Try it yourself and download the sample here.
If you have any questions, comments or issues related to this tutorial, please leave in the comments below.