Skip to content
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

Required Must Select #1070

Open
ghost opened this issue Nov 14, 2022 · 1 comment
Open

Required Must Select #1070

ghost opened this issue Nov 14, 2022 · 1 comment

Comments

@ghost
Copy link

ghost commented Nov 14, 2022

Hi Sir,

I hope you are doing well.

I have a request about REQUIRED, I wanna know how we can check js-choice If not select any value then give an error message and after selection error message hides on submit button.

Thanks

@ghost ghost added the bug label Nov 14, 2022
@BrayanCaro
Copy link

BrayanCaro commented Feb 6, 2023

I suggest to sync the validation message of the select element with the choicesjs input, which is the only visible element.

<select id="select" required>
    <option value="">select something</option>
    <option value="a">A</option>
    <option value="b">B</option>
</select>
const element = document.getElementById('select')
new Choices(element, {
    callbackOnInit() {
        const input = element.closest('.choices').querySelector('input')
        // trigger by a form o by using element.checkValidity
        element.addEventListener('invalid', () => input.setCustomValidity(element.validationMessage))
        element.addEventListener('change', () => input.setCustomValidity(element.validationMessage))
    },
})

Note: By default, the select element is hidden with display: none, that's why the required message is not displayed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants