validate(value, propertyName)
Validates the specified field and updates the state if the validation is successful.
The validation status is passed to the component through the
validatemessage
prop parameter. If the validation is
successful, an empty string is returned. If the validation fails, the
validatemessage
prop parameter contains the error message.
In addition to directly invoking the validate(value, propertyName) API, the field is validated automatically when the onBlur and/or onChange callbacks provided in the component prop parameter are invoked. These callbacks are either passed to the display component or called from the DX component.
Use the following code in the DX component for automatic validation where the display component invokes the callbacks provided in the props:
const {label, required, validatemessage, status, onChange, onBlur, readOnly, testId} = props;
<TextField
onChange={onChange}
onBlur={onBlur}
label={label}
,,,
/>
There are two types of client validations:
- Required - This validation prevents submitting empty fields.
- Type - This validates the field based on its type.
The following type validators are supported:
- alpha
- alphaNumeric
- alphaNumericWithSpace
- datetime
- futureDate
- integer
- nonNegative
- nonNegativeInteger
- notFutureDate
- phone
- posDecimal
- required
- urgencyValue
- url
- validators
- validPhoneNumber
Returns
An object containing the validation status.
Parameters
Name | Type | Description | Required |
value | string | The value of the field to be validated. | ✅ |
propertyName | string | The name of the field to be validated. | ❌ |
Usage example
In this example, the API returns an object that contains the validation status as false and an error message that conveys that the entered email address is invalid.
pConnect.getValidationApi().validate('abcgmail.com', '.email');
Previous topic APIs in the ValidationApi class Next topic Accessing APIs from the PCore object