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

<select> with attribute multiple does not return an array in validation and submit #3305

Open
williamthome opened this issue Feb 22, 2023 · 1 comment

Comments

@williamthome
Copy link
Contributor

For <select> HTML elements, LiveValidation only returns the first option selected even if the attribute multiple is set (<select multiple></select>). See in livevalidation,js, line 385:

case LiveValidation.SELECT:
            if (this.element.selectedIndex >= 0) return this.element.options[this.element.selectedIndex].value;
            else return "";

I think it needs to be:

case LiveValidation.SELECT:
            if (this.element.selectedIndex >= 0) return $(this.element).val(); // jQuery returns an array when multiple is set
            else return this.element.getAttribute("multiple") ? [] : "";

Also, same problem with the submit.
In zotonic-wired.js, line 1854, the check is weird for me:

var one = (t == 'select-one');

I think it needs to check for falsy value for the el.getAttribute("multiple").

Offtopic but related

  • Validation and Submit do not always produce/check the same value (checkbox input is an example);
  • Checkbox values are strange to me, it returns array or undefined. I think it should be an array only if more elements with the same name exist in the form and should return an empty array, not undefined.

I can do a PR to continue this discussion with some code, but I first want to hear what you think about this @mworrell.

@mworrell
Copy link
Member

Good one.

How should we use validation on a multi-value select element?

For the post it is simpler, just multiple times the key/value combination. At least I think that is the correct behavior, bit like a multi-checkbox.

I agree with the array/undefined. That is weird and very unexpected.

This whole LiveValidation might need an update, but that is for another time...

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