I have an interview using the mandatory code block approach (i.e. where the mandatory code block tells DA which questions to ask, as here: https://docassemble.org/docs/logic.html#bplogic).
Everything has been going swimmingly, except now I am trying to determine if a question should be asked, based on the value generated by the preceding question using states_list()
(https://docassemble.org/docs/functions.html#states_list).
So I have something like this:
---
mandatory: True
code: |
country_code = country
ask_for_state = bool(states_list(country_code))
if ask_for_state:
state
You can see I've gone down the rabbit-hole because I'm using bool()
to define ask_for_state
. his works fine for most countries (the US, Canada, France, the UK, etc.), so state
is being defined correctly. What's weird/unexpected is that even though ask_for_state
is false
in the raw data ("ask_for_state": false
), I'm still getting the question for state
popping up right after selecting country
.
All I can think is that DA is "pre-queuing" the question, so that when it's asking for country
, it has already decided it's going to ask for state
.
Not the behavior I want, because for example Saint Pierre and Michelon have no states, so you get really weird behavior in terms of the way the question displays (it essentially asks you to select a state from a null list, not to mention subdivision_type(country)
displays "None".
Wondering if anyone can lend any insight?
PS - I also noticed an issue where subdivision_type()
for Singapore returns 'division' with a lower-case 'd', which is sort of weird because "State," "Province," etc. all come in with the first letter capitalized.
Edit (2022-Jul-16): I feel silly... later on, I was accidentally asking for state
. Always nice to have a second set of eyes, thank you! :)
When the question for state
shows, you could press the </>
button to see the explanation of "How question came to be asked." It might be that the need for state
was triggered by something other than your mandatory
code
block.
In the following interview, the Mako if
statement ensures that the question for user_state
is not asked if the country doesn't have subdivisions.
question: |
In which country do you live?
fields:
- Country: user_country
code: countries_list()
default: US
---
question: |
Where do you live?
fields:
- State: user_state
code: states_list(country_code=user_country)
---
mandatory: True
question: |
Your country is ${ country_name(user_country) }.
% if len(states_list(country_code=user_country)) > 0:
You live in
${ state_name(user_state, country_code=user_country) },
which is abbreviated
${ user_state }.
% endif