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

Form templatetags: raise exceptions when unable to infer an enum #1540

Open
davidmiller opened this issue Jun 14, 2018 · 3 comments
Open

Form templatetags: raise exceptions when unable to infer an enum #1540

davidmiller opened this issue Jun 14, 2018 · 3 comments

Comments

@davidmiller
Copy link
Member

Opal form templatetags attempt to infer enums. They look at Django Choices, Opal Lookuplists or arbitrary JSON passed in the template.

Two enhancements would improve this functionality:

  • When passed a Boolean/NullBoolean field, know that the options are e.g. Yes/No/Unknown
  • When a widget that requires an enum (Select, Radio) can't find one, raise an exception rather than rendering an empty widget
@rossjones
Copy link
Contributor

Presumably, Unknown is only an option for NullBoolean fields?

For choice fields, it appears that return [i[1] for i in choices] returns only the display string part of the tuple, how does it find the values it can use (for the bool it should probably be True, False, None).

@fredkingham
Copy link
Contributor

At the moment we just save the display part of the string because tbh that's just the only usecase we could conceive of wanting.

So this will require some value work on the radio buttons https://docs.angularjs.org/api/ng/input/input%5Bradio%5D

yes please for the None/Unknown plan.

rossjones added a commit that referenced this issue Jun 18, 2018
Where a field is a BooleanField or a NullBooleanField inference should
generate a lookup list with `[Yes, No]` and `[Yes, No, Unknown]` as
options (for radio buttons).

This will require changes to templates/radio.html so that Unkown does
not use Unknown as a value, instead leaving the value empty so that we
know the intended value is None.

This would be easier if lookuplists were tuples (like choices in Django)
so I'm considering implementing support for lists of tuples (as well as
the current list of strings) for lookuplists - the alternative is too
much logic in the template and special-casing the string 'Unknown'

Will fix #1540
@fredkingham fredkingham self-assigned this Oct 29, 2018
@fredkingham
Copy link
Contributor

I think we need to rethink this. There are 2 options.

Move logic into the field translator and do a hard coded if field type == Boolean or NullBoolean then translate yes, no, unknown accordingly.

Use logic similar to what is included here but change it so that we use a dictionary which populates value. I'm pro the latter as it doesn't require angular. The problem is any instances where we have hardcoded lookup list properties that currently takes a string that we include as javascript in the template.

Its an interesting one, but lets hold off for the time being.

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

4 participants