Mapped input field should only contain positive amounts

I have a template that is populated with mapped accounts. How do I make sure that my fields only contain positive amounts, i.e. if a mapped account is -X it should be displayed as X in the field. Thanks.

Hi @cbunger, welcome back to the Silverfin community.

So you are trying to set the value of the accounts as the default value to be shown in the inputs fields, and this values should always be positives. Am I right?

In that case and to make sure that these values are shown as absolute values, you could use the ‘abs’ filter, adding and extra step before the input field is created, like next:

{% assign account_abs_value = account.value | abs %}
{% input custom.some.input_field as:currency default:account_abs_value %}

This will make all the values taken from the accounts to be positive, but it won’t prevent the user to manually input a negative value. Which unfortunately it is not possible to limit in the input fields at the moment. Considering this situation, and if the user should not input negative values, you could display a message if they do it. For example:

{% if custom.some.input_field < 0 %}
{% ic %}{::warningtext}Value must be positive{:/warningtext}{% endic %}
{% endif %}

I hope this explanation help you with you issue. If you still have any concerns, feel free to share you code here so we can take a look at that specific situation. :slight_smile:

Best regards,
Agustín.

Hi Agustin,

Thanks for your response. The fields get populated via a loop through an account array. My code looks like this:

{% assign cy_value = period.accounts | range:column1_account_array[forloop.index0] %}
{% $1+ cy_value %}
|]^_ {% input custom.value.[item] default:cy_value as:currency %} _^[

So what I want to achieve is that the input value is positive. When I add “| abs” in the syntax line for assign cy_value, all fields are emptied.

Try with the next code:

{% assign cy_value = period.accounts | range:column1_account_array[forloop.index0] %}
{% assign cy_value_abs = cy_value.value | abs %}
{% $1+ cy_value %}
|]^_ {% input custom.value.[item] default:cy_value_abs as:currency %} _^[

So, the situation here is this. In the first line:

{% assign cy_value = period.accounts | range:column1_account_array[forloop.index0] %}

You are assigning the account drop to cy_value (not only the value of the account).

And in the next step:

{% assign cy_value_abs = cy_value.value | abs %}

With the added “.value” you are accessing to the value information of the account, and then applying the abs filter.

In the next link is the list of all the methods you can apply to accounts (e.g. .value, .number, .count, etc.)

One additional thing you should consider its the line about the register: if you should use the original value or the absolute value (probably the absolute one?):

{% $1+ cy_value %} or {% $1+ cy_value_abs %}

And finally, ussing the new variable with absolute values as a default of the input:

|]^_ {% input custom.value.[item] default:cy_value_abs as:currency %} _^[

As I commented before, you won’t be able to avoid users to manually input a negative value, and for that reason I suggested using some kind of warning or error message in that case.

1 Like