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

HTML select "IndexError: tuple index out of range" when using custom setter with "$event.target.value" #628

Open
pa-hqt opened this issue Nov 6, 2023 · 1 comment

Comments

@pa-hqt
Copy link

pa-hqt commented Nov 6, 2023

If you try to use a custom setter method on a select in combination with "$event.target.value" (as mentioned in the docs), the setter raises an exception if you try to select the empty value (value=""), because the value is not transmitted to the backend.
None empty values work as expected.
But in case of an empty value the actionQueue.0.payload.name is set_flavor() instead of set_flavor('')

The exception is:
File "django-unicorn\django_unicorn\views\action_parsers\call_method.py", line 157, in _call_method_name
parsed_args.append(cast_value(type_hint, args[len(parsed_args)]))
IndexError: tuple index out of range

HTML example:

<select unicorn:change="set_flavor($event.target.value)" id="select-custom-setter">
      <option value="">-- select --</option>
      {% for flavor in flavors %}
      <option value="{{ flavor }}">{{ flavor }}</option>
      {% endfor %}
</select>

Component example:

from typing import Optional
from django_unicorn.components import UnicornView

class HtmlInputsView(UnicornView):
    flavor: Optional[str] = None
    flavors = ["flavor 1", "flavor 2"]

    def set_flavor(self, value: str):
        self.flavor = value

In my case the error still exists if I change the custom setter definition to set_flavor(self, value: str = "") with a default value.

Minimal example (extension of your html-input example):
example_to_demonstrate_a_bug,_if_you_use_a_custom_setter_function_that_is_executed.patch

@NotAnyMike
Copy link

Same

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

No branches or pull requests

2 participants