@progress/kendo-angular-barcodes
Version:
Kendo UI Angular Barcodes
45 lines (44 loc) • 1.54 kB
JavaScript
/**-----------------------------------------------------------------------------------------
* Copyright © 2025 Progress Software Corporation. All rights reserved.
* Licensed under commercial license. See LICENSE.md in the project root for more information
*-------------------------------------------------------------------------------------------*/
import { qrcodeValidator } from '@progress/kendo-charts';
/**
* Creates a value validator for a particular Barcode type.
*
* @param {QRCodeEncoding} encoding The QR Code encoding. Defaults to 'ISO_8859_1'.
* @returns {ValidatorFn} A validator function that returns an error map with the `qrcode` property if the validation check fails, otherwise `null`.
*
* @example
* ```ts-no-run
* const control = new FormControl('Фоо', createQRCodeValidator());
* console.log(control.errors);
*
* // {
* // qrcode: {
* // message: 'Unsupported character in QR Code: "Ф".',
* // value: '1234',
* // type: 'EAN13'
* // }
* // }
* ```
*/
export const createQRCodeValidator = (encoding = 'ISO_8859_1') => {
const validator = qrcodeValidator(encoding);
return (control) => {
if (!control.value) {
return null;
}
const result = validator(control.value);
if (result.valid === true) {
return null;
}
return {
qrcode: {
message: result.error.message,
value: control.value,
encoding: encoding
}
};
};
};