Skip to content

334: Improve error message when a question's label and/or hint is missing - #893

Merged
lognaturel merged 17 commits into
XLSForm:masterfrom
lindsay-stevens:pyxform-334
Dec 3, 2026
Merged

lognaturel merged 17 commits into
XLSForm:masterfrom
lindsay-stevens:pyxform-334

Conversation

@lindsay-stevens

Copy link
Copy Markdown
Contributor

Closes #334
Closes #760

Why is this the best possible solution? Were any other approaches considered?

Aimed to simplify and consolidate the decision making logic, for whether a question will be visible, or whether it requires label/hint/media. Addressing #334 was not as straightforward as it initially seemed, since the requirement for label/hint/media can be affected by the question type, calculation, trigger, and appearance. Since #760 was in a quite similar area, it is addressed here too. Most of the commits are setup for the final few that implement the changes, e.g. adding test coverage or constants, to help clarify where relevant code is and what would be safe enough to change.

Maybe the code in question.py for building form controls could be further optimised but I wanted to be sure about the structure in this PR first. For example question types that map to the base Question class aren't getting a control element anyway, so the default implementation could return None; rename "build_xml" to "xml_control" and putting the setvalue code into a method that relevant subclasses can call before returning; move the setgeopoint code into the GeoQuestion class; etc.

What are the regression risks?

Hopefully this is closing off a bunch of regressions! For example a Question.xml_control condition focused on the "calculate" type but allowed question triggers targeting other non-visible question types. Maybe users are relying on that behaviour but it seemed from the related docs/errors/PRs/issues/commits that it was not something that would work.

Does this change require updates to documentation? If so, please file an issue here and include the link below.

No

Before submitting this PR, please make sure you have:

  • included test cases for core behavior and edge cases in tests
  • run python -m unittest and verified all tests pass
  • run ruff format pyxform tests and ruff check pyxform tests to lint code
  • verified that any code or assets from external sources are properly credited in comments

@lindsay-stevens

Copy link
Copy Markdown
Contributor Author

Rebased to resolve merge conflict from PR 896. Applying constants.CALCULATE to builder.py in 21346a3 was superseded by commit bac5cbc that also reorganised an if block there (which caused the conflict).

@lognaturel lognaturel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I like the direction. I've flagged a few small things I have questions about.

I will need a bit more time to think about some of the deeper changes. I think it would be ideal to get in the release since it's a major version bump already and there are potentially-breaking changes.

I believe some callers start from json representations of forms. Is that a usage you've identified or am I remembering wrong?

)

def test_validate_big_image_with_no_image__with_columns__ok(self):
"""Should find no error is raised if there is big-image/image in at least one language."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sorry this is a question I meant to write up in the PR. The current check follows the previous implementation, and this test documents a gap in it. The error message reflects what I think was/is intended. Though when implementing the per-language check maybe a different and/or more specific error message would be useful, e.g. to say which language(s) are missing an image for big-image. Would you prefer I implement here or in a follow-up PR?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Let's handle it separately in a new issue, it is a refinement. Agreed a more specific error message would be helpful.

Comment thread pyxform/errors.py Resolved
Comment thread tests/test_choices_sheet.py Resolved
@lindsay-stevens

Copy link
Copy Markdown
Contributor Author

I believe some callers start from json representations of forms. Is that a usage you've identified or am I remembering wrong?

Yes recently I did an inventory of this in #887. The use case isn't well defined/supported, so it's a best effort thing trying not to break existing tests. The idea of moving validations out of SurveyElement classes is that this layer (to me) is about serialisation, primarily to XForm. User-facing error messages or warnings should happen earlier, in the transform step coordinated in xls2json.py, particularly since the source sheet/row/column references are only available there.

Comment thread pyxform/survey_element.py
and self.control.get("appearance") == "label"
and not self.label
):
self.label = " "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now <label/> might be generated, right? Are we sure that works for Collect and Web Forms?

@lindsay-stevens lindsay-stevens Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would be an empty label element instead of a blank <label> </label>. There are examples of an empty label element in the Collect v2026.4.0 tests for a group label and for an entity label, but I can't see examples (in search) for UI widgets like input or select1 so I'll check and report back.

In SurveyElement.xml_label there was (is still) a code path that leads to an empty label element (when not using itext and the label value is falsy/empty) which made this seem safe to do, though that's not evidence that it works in Collect.

@lognaturel lognaturel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one question about client compatibility that I think would be good to look into before merging. But other than that, looks good!

- the survey row data iterated on in the for-loop L450 is already a copy
  of the incoming data, due to dealias_and_group_headers. Also the row
  was being modified directly in many places already. This is to also
  make it easier to follow what data is added to parent_children_array.
- the previous `question_type == "a_type"` blocks contained control flow
  (continue) which makes it difficult to see where in the L450 loop to
  add new validations that would apply to all questions, but that runs
  after the question_type-specific data processing/validation had run
  (it would go in the L1274 `else` block, or after it).
  - unfortunately the `parent_children_array.append(row)` isn't easily
    able to be consolidated, since for select questions there may be
    auto-generated questions/fields inserted before and/or after the
    user-specified question.
- Does not seem to affect processing or tests. Might have been useful
  in regards to internally grouping these question types or setting some
  custom behaviour, but if that is/was needed then a distinct Question
  subclass would be clearer than mapping to the base class.
- also apply other existing constants to expressions being modified
  already by this commit.
- also apply other existing constants to expressions being modified
  already by this commit.
- note there are two things called "trigger" in pyxform:
  - question type: a checkbox that stores "OK" (aka "acknowledge")
  - survey column: one or more variable references for setvalue actions
- test module is specific to a question type
- similar pattern used in other files, makes it easier to see tests for
  validation/warning/error behaviour vs. XForm output
- add calculate test traceability tags/descriptions
- change parsing tests to focus on the error cases (plus a few extra
  seemingly useful) scenarios, rather than repeating output test cases
  - each output test has a `warnings_count=0` to retain that assertion
    from the old tests
  - move the non-calculate type tests for no control content into new
    test_fields.py test that covers more types, add cases for combining
    them with trigger and calculate, add an inverse test for types
    that ignore control content if specified, and scenarios for when
    any of these are referenced in a trigger column.
    - on a similar note add a groups test to show that in a `field-list`
      the control content is optional for a `label` appearance item,
      which is essentially the manual equivalent of `table-list`.
- create a similar set of 5 output test cases for 3 scenarios:
  - test cases: minimal columns, with label, with static default,
    with dynamic default, with dynamic default but no calculation
  - scenarios: question types "calculate", "integer", and "select_one"
    - calculate is assumed the base/default case for a string result
    - integer is an example of a different data type for the result
    - select use cases not as obvious but could be a kind of enum where
      the result is from a lookup/instance().
- Not necessarily ideal behaviour, but the test documents current state.
  - The check could instead look at each language and within them check
    that every non-blank 'big-image' has a matching non-blank 'image'.
  - Usage on choices sheet does not get checked since the validation
    is in SurveyElement.xml_label_or_hint, and that is not called when
    preparing XML for Question.Option (choices items class). But it
    seems the intention is to allow big-image with choices:

    XLSForm#635 (comment)
    "big-image can be used to provide a bigger image either for a
    question label or for a select choice."

    https://docs.getodk.org/form-question-types/#including-media-files-in-choices
    "For images, you can specify a bigger image for panning and zooming
    using the big-image column. This is not compatible with the
    no-buttons appearance."
- the check is for questions and choices (not every SurveyElement type)
  so this simplifies SurveyElement.
- previously not run for choices as shown in tests changes
- improve error message by adding more details and docs link
- in survey_element.py, for a select1 with the appearance `label`, a
  single space " " label is emitted if there is no label. Since this
  appearance only applies to types that map to MultipleChoiceQuestion,
  the logic is moved there, with the comment to explain the exception.
  - a constant for the appearance `label` is added to help differentiate
    it from the element constant `label` but at some stage probably this
    should be an enum, as was done for parameters.
- since xml_label_and_hint is only called from Question._build_xml, the
  relatively small amount of logic is moved there. To allow the above
  type-specific behaviour, `xml_label` is defined with the default
  conditions for creating a label.
- since xml_hint is only called from SurveyElement.xml_label_or_hint,
  it is moved to Question as well.
- update/expand error message
- applied to questions generated by pyxform (row_count,
  table-list header, specify-other text) in case they change in future
  and drift out of spec, considering that the old check would have
  applied to these items as well anyway.
- update/expand error message
- applied to questions generated by pyxform (row_count,
  table-list header, specify-other text) in case they change in future
  and drift out of spec, considering that the old check would have
  applied to these items as well anyway.
- the data in survey.setvalues_by_triggering_ref is from the trigger
  refs on each question, set via builder.py. In xls2json the global
  trigger list is available in `trigger_references`, so the same check
  can occur there.
- in question.py, the original condition essentially for "calculate, or
  non-calculate with calculation or trigger" seemed to be more of a
  general problem with visibility (as noted in the error message).
  - There are many other question types besides "calculate" which will
    not be visible (due to having no control element) and so should
    result in the same error. Evidence for that is in the test_fields.py
    test_non_visible__no_control_content__as_trigger__error where before
    it was allowed to use hidden/background-audio/etc as a trigger, but
    now that's an error.
  - Whether or not a non-calculate has a calculation or trigger then
    seemed to be not as important, since for example it's allowed to
    have label/hint/media for an integer question with a calculation
    (see test_calculate.py tests tagged CV003 and CB007). That may be a
    cause for a warning if the calculation expression isn't controlling
    evaluation timing such as with `once()`, per pyxform/XLSForm#546.
    - similarly a warning might be useful if label/hint/media is defined
      but that question type has no control i.e. can never be visible.
- seemed like the prior use of a single space was to work around logic
  in the Question or SurveyElement class that required the label value
  to be truthy. But now that the MultipleChoiceQuestion class has an
  opportunity to output a label directly, it would not need to do that.
  It seems that Collect ignores/trims blank space in labels anyway.
@lindsay-stevens

lindsay-stevens commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased to resolve merge conflicts:

@lognaturel

Copy link
Copy Markdown
Contributor

Empty labels verified in Collect and Web Forms, merging.

@lognaturel
lognaturel merged commit 104c673 into XLSForm:master Dec 3, 2026
26 of 28 checks passed
@lindsay-stevens
lindsay-stevens deleted the pyxform-334 branch December 4, 2026 06:02
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.

big-image without image does not throw error on choice option Improve error message when a question's label and/or hint is missing

2 participants