Condition in text field

Hello! I need to add a condition in the text field.

I want to get something like:

{{ text_field('test', 'class': 'input w-full border', 'value': contract.totalPrice|default(""), 'required': (contract.id is not empty) ? false : true) }}

but it does not work. there is some solution?

edited 11d ago

Maybe determine that before the field:

{% set required = true %}
{% if contract.id is not empty %}
    {% set required = false %}
{% endif %}

{{ text_field('test', 'class': 'input w-full border', 'value': contract.totalPrice|default(""), 'required': required) }}

Or, if you don't mind mixing in a little raw PHP code:

<?php $required = !isset($contract.id); ?>

{{ text_field('test', 'class': 'input w-full border', 'value': contract.totalPrice|default(""), 'required': required) }}


3.5k

thanks! I understand that you can do just that. but I think we can write the condition inside text_field.

Maybe determine that before the field:

{% set required = true %}
{% if contract.id is not empty %}
  {% set required = false %}
{% endif %}

{{ text_field('test', 'class': 'input w-full border', 'value': contract.totalPrice|default(""), 'required': required) }}

Or, if you don't mind mixing in a little raw PHP code:

<?php $required = !isset($contract.id); ?>

{{ text_field('test', 'class': 'input w-full border', 'value': contract.totalPrice|default(""), 'required': required) }}