The validation data store provides a way to show errors for fields in the Cart or Checkout blocks.
The data in the store should be a single object, the keys of which are the error IDs and values are the data associated with that error message. The values in the object should contain message and hidden. The message is the error message to display and hidden is a boolean indicating whether the error should be shown or not.
An example of how the data should be structured:
{
"error-id-1": {
message: "This is an error message",
hidden: false,
},
"error-id-2": {
message: "This is another error message",
hidden: true,
},
}
When the checkout process begins, it will check if this data store has any entries, and if so, it will stop the checkout process from proceeding. It will also show any errors that are hidden. Setting an error to hidden will not clear it from the data store!
Returns the validation error.
- errorId
string
- The error ID to get validation errors for.
const store = select( 'wc/store/validation' );
const billingFirstNameError = store.getValidationError( 'billing-first-name' );
object
: The validation error which is an object containing message (string
) and hidden (boolean
).
Gets a validation error ID for use in HTML which can be used as a CSS selector, or to reference an error message. This will return the error ID prefixed with validate-error-
, unless the validation error has hidden
set to true, or the validation error does not exist in the store.
- errorId
string
- The error ID to get the validation error ID for.
string
: The validation error ID for use in HTML.
Returns true if validation errors occurred, and false otherwise.
boolean
: Whether validation errors occurred.
Clears a validation error.
- errorId
string
- The error ID to clear validation errors for.
const store = dispatch( 'wc/store/validation' );
store.clearValidationError( 'billing-first-name' );
Clears multiple validation errors at once. If no error IDs are passed, all validation errors will be cleared.
- errors
string[] | undefined
- The error IDs to clear validation errors for. This can be undefined, and if it is, all validation errors will be cleared.
- This will clear only the validation errors passed in the array.
const store = dispatch( 'wc/store/validation' );
store.clearValidationErrors( [
'billing-first-name',
'billing-last-name',
'terms-and-conditions',
] );
- This will clear all validation errors.
const store = dispatch( 'wc/store/validation' );
store.clearValidationErrors();
- errors
object
: An object containing new validation errors, the keys of the object are the validation error IDs, and the values should be objects containing message (string
) and hiddenboolean
.
Sets the validation errors. The entries in errors will be added to the list of validation errors. Any entries that already exist in the list will be updated with the new values.
const { dispatch } = wp.data;
const { setValidationErrors } = dispatch( 'wc/store/validation' );
setValidationErrors( {
'billing-first-name': {
message: 'First name is required.',
hidden: false,
},
'billing-last-name': {
message: 'Last name is required.',
hidden: false,
},
} );
Hides a validation error by setting the hidden
property to true
. This will not clear it from the data store!
- errorId
string
: The error ID to hide.
const { dispatch } = wp.data;
const { hideValidationError } = dispatch( 'wc/store/validation' );
hideValidationError( 'billing-first-name' );
Shows a validation error by setting the hidden
property to false
.
- errorId
string
: The error ID to show.
const { dispatch } = wp.data;
const { showValidationError } = dispatch( 'wc/store/validation' );
showValidationError( 'billing-first-name' );
Shows all validation errors by setting the hidden
property to false
.
const { dispatch } = wp.data;
const { showAllValidationErrors } = dispatch( 'wc/store/validation' );
showAllValidationErrors();
We're hiring! Come work with us!
🐞 Found a mistake, or have a suggestion? Leave feedback about this document here.