django-unicorn icon indicating copy to clipboard operation
django-unicorn copied to clipboard

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

Open pa-hqt opened this issue 2 years ago • 1 comments

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

pa-hqt avatar Nov 06 '23 09:11 pa-hqt

Same

NotAnyMike avatar Apr 01 '24 20:04 NotAnyMike