field().error()
Show / hide error messages for a field.
Please note - this property requires the Editor extension for DataTables.
Description
If invalid data is entered into a field, you will likely want to show an error message for that field telling the user what the error is and how they can correct it - client-side validation is a typical example of where this might be useful.
This method provides that ability, giving a way to set or remove an error message for a field.
Note that when submitting data to the server, the process on the server will also have to perform its own validation, and can report field errors to the client-side. When it does so, this method is used to display the error message that the server returns for the field.
Note that the error() method also provides the ability to show and hide an error message for a specific field, and also extends that ability to being able to set a form level error message. For field errors, these two methods can often be used interchangeably. The key difference is in the return value - this method will return a DataTable.Editor.Field instance, while the error() method returns a DataTable.Editor instance. This can affect how you chain methods if you are using the returned value for chaining.
Type
field(…).error( msg )
Show or hide an error message for a field
Parameters:
| Name | Type | Optional | |
|---|---|---|---|
| 1 | msg | No | |
Error message to show for the field. To remove an existing message, pass in an empty string ( As of Editor 3.0, it is possible to pass | |||
Returns:
DataTable.Editor.Field: Field instance
Example
Client-side validation using the preSubmit event and showing an error message.:
editor.on('preSubmit', function (e, d) {
var usernameField = this.field('username');
if (usernameField.val() === '') {
usernameField.error('A user name is required');
return false;
}
return true;
});Related
The following options are directly related and may also be useful in your application development.