Skip to content

Fix 0 value IntegerField in TemplateHTMLRenderer#5768

Closed
nikhil96sher wants to merge 1 commit intoencode:masterfrom
nikhil96sher:issue-5767
Closed

Fix 0 value IntegerField in TemplateHTMLRenderer#5768
nikhil96sher wants to merge 1 commit intoencode:masterfrom
nikhil96sher:issue-5767

Conversation

@nikhil96sher
Copy link
Copy Markdown

@nikhil96sher nikhil96sher commented Jan 24, 2018

Signed-off-by: Nikhil Sheoran nikhilsheoran96@gmail.com

Note: Before submitting this pull request, please review our contributing guidelines.

Description

Fixes #5767
The current implementation of TemplateHTMLRenderer's input.html for various template packs checked {% if field.value %} which gave incorrect validation for IntegerField with 0 as input. This PR fixes it by checking {% if field.value is not None and field.value != "" %}

Signed-off-by: Nikhil Sheoran <nikhilsheoran96@gmail.com>
@nikhil96sher
Copy link
Copy Markdown
Author

The second check is required because utils.serializer_helpers.BoundField.as_field_error() converts None to '' (caught by TestNestedBoundField.test_rendering_nested_fields_with_none_value )

@nikhil96sher nikhil96sher changed the title [WIP] Fix 0 value IntegerField in TemplateHTMLRenderer Fix 0 value IntegerField in TemplateHTMLRenderer Jan 24, 2018
@xordoquy
Copy link
Copy Markdown
Contributor

I need to ensure this won't have side effets on non integer fields that rely on that template.

@nikhil96sher
Copy link
Copy Markdown
Author

@xordoquy Should I also include tests in the PR for the remaining non-integer fields that uses the input.html?

@kgeorgy
Copy link
Copy Markdown

kgeorgy commented Jan 29, 2018

We can list which falsy expressions led to no "value" rendering before:

  • None => In the current solution: no "value" attribute : OK
  • False => In the current solution: ? Is there case where we could have a boolean value rendered with an html input? In the current solution there will be a "False" text as value. (OK?)
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j: value is rendered as 0: OK
  • any empty sequence, for example, '', (), []: empty string => no value : OK. What should we have for [] and ()?
  • any empty mapping, for example, {}: Mapping in the value (OK?)

Maybe, to be sure to only have impact when the value is a numeric 0 value should we test :

{% if field.value == 0 or field.value %}...{% endif %} (Absolutely not tested, from scratch coding) ?

@lovelydinosaur
Copy link
Copy Markdown
Contributor

I'd suggest we just change it to {% if field.value is not None %}
At that point I'd consider this fixed.

We can list which falsy expressions led to no "value" rendering before:

I don't really care about what False, (), {} render to in there, since they shouldn't map on to text inputs anyways, and we can't properly represent them when they do.

@nikhil96sher
Copy link
Copy Markdown
Author

@tomchristie As mentioned in the description as well, the second check is required because utils.serializer_helpers.BoundField.as_field_error() converts None to '' (caught by the test TestNestedBoundField.test_rendering_nested_fields_with_none_value )
@kgeorgy A mapping wouldn't render on the input.html template, but instead uses the fieldset.html template and recursively calls the parser for each key of the map.

@kgeorgy
Copy link
Copy Markdown

kgeorgy commented Jan 29, 2018

@tomchristie It makes sense to me too. I just wanted to list all the possible impacts the change has.
@nikhil96sher I know a mapping/list/ or other object is not intended to be rendered in a simple html input field, this is just to consider all the cases, and decide what to do with. A "we don't care" answer here is completely OK for me :)

@nikhil96sher
Copy link
Copy Markdown
Author

@tomchristie Any updates on this?

@carltongibson
Copy link
Copy Markdown
Collaborator

carltongibson commented Feb 16, 2018

I'd suggest we just change it to {% if field.value is not None %}
At that point I'd consider this fixed.

If the PR is updated (to the simpler solution) it should be good to go.

@carltongibson
Copy link
Copy Markdown
Collaborator

Closing in favour of #5834. Thanks @nikhil96sher!

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

Successfully merging this pull request may close these issues.

IntegerField with a 0 value is not rendered properly with TemplateHTMLRenderer

5 participants