-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: max-length property not being set in input component #727
Comments
@leblanccr thanks for your feedback! The
You can create a validator for the component where you can specify a max-length, for example. Let us know if you want more information on that. |
Hi Daine,
Thanks for the info.
If you don’t mind, could you elaborate on creating a custom validator? I’d really like to know more about that.
Thanks,
Craig
From: Daine Trinidad ***@***.***>
Sent: Thursday, January 9, 2025 3:48 AM
To: cds-snc/gcds-components ***@***.***>
Cc: LeBlanc, Craig (DFO/MPO) ***@***.***>; Mention ***@***.***>
Subject: Re: [cds-snc/gcds-components] bug: max-length property not being set in input component (Issue #727)
You don't often get email from ***@***.******@***.***>. Learn why this is important<https://aka.ms/LearnAboutSenderIdentification>
@leblanccr<https://github.com/leblanccr> thanks for your feedback! The size property for the gcds-input component does not set the max-length for the input field. This is done for accessibility and usability reasons. We are in the process of updating our documentation in github to reflect this. This is the new description for the property
Size attribute for an input element to provide a visual indication of the expected text length to the user
You can create a validator for the component where you can specify a max-length, for example. Let us know if you want more information on that.
—
Reply to this email directly, view it on GitHub<#727 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BHR4XXMCRXSL32ARVYNGPXD2JYSUNAVCNFSM6AAAAABU2UMBY2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKNZZGM3DMMJQHE>.
You are receiving this because you were mentioned.Message ID: ***@***.******@***.***>>
|
@leblanccr <gcds-input
input-id="form-name"
label="Validation test"
name="validator-test"
hint="Must be between 3 and 10 characters."
id="text-input"
size="10"
required
></gcds-input>
<script>
// Custom validator to allow validation min length, max length or value between min and max
function getLengthValidator(min, max) {
// Create errorMessage object
let errorMessage = {};
if (min && max) {
errorMessage["en"] = `You must enter between ${min} and ${max} characters`;
errorMessage["fr"] = `French You must enter between ${min} and ${max} characters`;
} else if (min) {
errorMessage["en"] = `You must enter at least ${min} characters`;
errorMessage["fr"] = `French You must enter at least ${min} characters`;
} else if (max) {
errorMessage["en"] = `You must enter less than ${max} characters`;
errorMessage["fr"] = `French You must enter less than ${max} characters`;
}
return {
validate: (value) => {
value = value || '';
if (min && max) {
return min <= value.length && value.length <= max;
}
if (min) {
return min <= value.length;
}
if (max) {
return value.length <= max;
}
return true;
},
errorMessage
};
}
// Get the text input
const textInput = document.getElementById('text-input');
// Assign the validator
textInput.validator = [ getLengthValidator(null, 10) ];
</script> This custom validator allows you to set a min and max length for the entered value and have a specific error message depending on what you have passed. If using a framework like React/Vue, you can also assign the validators to the components with a property like The date input is formatted a little differently since it is a component that has three fields built into it. If you want a code example for the date input component, let us know! |
Prerequisites | Prérequis
GC Design System Components Package and Version | Paquet et version des composants de Système de design GC
gcds-components-vue 0.26.2
Current Behavior | Comportement observé
The size property in the gcds-input component only sets the width of the input field and not a character limit.
Expected Behavior | Comportement attendu
The size property in the gcds-input component should set a character limit for the input field.
System Info | Information sur le système
Steps to Reproduce | Étapes pour reproduire le bogue
Code Reproduction URL | URL de reproduction du code
No response
Additional Information | Informations supplémentaires
No response
The text was updated successfully, but these errors were encountered: