Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

Validate User-Entered Data before Committing it to a Cell in React

Background:

You may have requirements to prevent users from entering certain data into a cell (for example, preventing users from entering data values lower than zero). By using cellEditEnding, you can validate the data before committing it to a cell on the grid.

Steps to Complete:

1. Bind the cellEditEnding event to a method

2. Check to see if the data meets your requirements

Getting Started

Bind the cellEditEnding event to a method

First, we must set up the FlexGrid by initializing it with data, and then bind a method to the cellEditEnding event of the grid.

<wjGrid.FlexGrid itemsSource={this.state.data} initialized={this.initGrid.bind(this)} cellEditEnding={this.validateData.bind(this)}>. . . .</wjGrid.FlexGrid>

Check to see if the data meets your requirements

Next, we check to see if the user-entered data matches the criteria to be saved in the cell. In this sample, we'll check to see if the numeric value entered is less than 0. If it is less, then we discard it; if not, we won't need to do anything and the grid will take care of updating itself with the new data.

validateData(s, e) {
    if(e.col == 3) {
        if(s.activeEditor.value < 0) {
            alert('Invalid data. Amount cannot be less than 0.');
            e.cancel = true;
        }
    }
}

You can find a live sample of this project here: https://stackblitz.com/edit/wijmo-knowledgebase-react-flexgrid-data-validation

Get the Latest Version of Wijmo
Download the Free Trial Now