Required fields not working correctly in fori loops

It seems that when I put a required input into a fori loop it is not marked with a red outline. Here’s my (simplified) code:

{% fori item in custom.risks %}
  {% assign RGS_code = item.RGS %}
  {% capture filter_accounts %}
    {% input custom.RGS_filtering.value as:account_collection range:"B,W" default:"B,W" accounts_var:accounts_used %}
  {% endcapture %}
  {% assign level2_options = "" %}
  {% assign level2_value = "" %}
  {% for account in accounts_used %}
    {% assign account_sliced = account.number | slice:0,4 %}
    {% unless level2_value contains account_sliced %}
      {% capture option %}{% t account_sliced %}|{% endcapture %}
      {% capture option_value %}{{ account_sliced }}|{% endcapture %}
      {% assign level2_options = level2_options | append:option %}
      {% assign level2_value = level2_value | append:option_value %}
    {% endunless %}
  {% endfor %}
  {% newline %}
  {% capture RGS_code %}{{ item.RGS }}{% endcapture %}
  {% assign i = 0 %}
  {% capture RGS_code_i %}{{ item.RGS }}{{ i }}{% endcapture %}
  {% assign RGS_range = period.accounts[RGS_code] | map:"number" | join:","  %}
  {% capture filter_accounts %}
    {% input custom.RGS_filtering.value as:account_collection range:RGS_range default:RGS_range accounts_var:accounts_used %}
  {% endcapture %}
  {% assign level3_options = "" %}
  {% assign level3_value = "" %}
  |{% input item.RGS as:select options:level2_options option_values:level2_value %}
  |{% input item.Risk_description as:text %}
  |{% input custom.[RGS_code].risk_remark as:text placeholder:placeholder_flagged required:true %}
{% endfori %}

Now, the last input field should me marked read for all but the last line right? But this doesn’t happen. When I change my code to:

{% for item in custom.risks %}
  {% assign RGS_code = item.RGS %}
  {% capture filter_accounts %}
    {% input custom.RGS_filtering.value as:account_collection range:"B,W" default:"B,W" accounts_var:accounts_used %}
  {% endcapture %}
  {% assign level2_options = "" %}
  {% assign level2_value = "" %}
  {% for account in accounts_used %}
    {% assign account_sliced = account.number | slice:0,4 %}
    {% unless level2_value contains account_sliced %}
      {% capture option %}{% t account_sliced %}|{% endcapture %}
      {% capture option_value %}{{ account_sliced }}|{% endcapture %}
      {% assign level2_options = level2_options | append:option %}
      {% assign level2_value = level2_value | append:option_value %}
    {% endunless %}
  {% endfor %}
  {% newline %}
  {% capture RGS_code %}{{ item.RGS }}{% endcapture %}
  {% assign i = 0 %}
  {% capture RGS_code_i %}{{ item.RGS }}{{ i }}{% endcapture %}
  {% assign RGS_range = period.accounts[RGS_code] | map:"number" | join:","  %}
  {% capture filter_accounts %}
    {% input custom.RGS_filtering.value as:account_collection range:RGS_range default:RGS_range accounts_var:accounts_used %}
  {% endcapture %}
  {% assign level3_options = "" %}
  {% assign level3_value = "" %}
  |{% input item.RGS as:select options:level2_options option_values:level2_value %}
  |{% input item.Risk_description as:text %}
  |{% input custom.[RGS_code].risk_remark as:text placeholder:placeholder_flagged required:true %}
{% endfor %}

It works like a charm. Is this expected behavior, am I doing something wrong or is this a bug?

Hi @ronald_groot_RSF ,

Whenever you use this required attribute in inputs related to the fori, it will only work correctly for the fori itself. In your example the input is not related to the fori (because of the use of the custom drop custom.[RGS_code].risk_remark). This doesn’t seem to work as it tricks the code to think we’re in the last iteration of the loop (which in case of fori-structure is always empty.

Can we ask why did you choose to include a custom drop in the fori structure? Alternatives like input item.[RGS_code] might give the exact same output and required would work fine in that case.

Furthermore, you could combine this with the use of the persisted method to make it more clear for users from a functional perspective (CASE: use persisted to hide parts of a loop).

Let me know if this is clear!

Kind regards,
Robin

Hi Robin,

Thanks for the help. The reason was a bit of an example of lazy coding I’m afraid… There was a custom.[RGS_code].value I needed, since this needed to be shown in other templates as well and I just copy pasted it, because I wanted to use 3 levels and item.[RGS_code].risk_remark generates and error on that. But I worked around that and now my required field is shown nicely!