0

In a form, I have this field:

$form['payment'] = [
  '#type' => 'radios',
  '#title' => $this->t('Payment Mode'),
  '#options' => [
    'chk' => $this->t('by check'),
    'tfr' => $this->t('by bank transfer'),
    'crd' => $this->t('by card'),
  ],
  '#required' => TRUE,
];

In the Twig template associated to the form, I'd like to show the field and some html stuff depending on the selected option. I tried this:

<form{{ attributes }}> {{ form.form_build_id }} {{ form.form_token }} {{ form.form_id }}
  <div>
    {{ form.payment }}
    {% if form.payment ==  'chk' %}
      You chose 'Check'
    {% elseif form.payment ==  'tfr' %}
      You chose 'Bank Transfer'
    {% elseif form.payment ==  'crd' %}
      You chose 'Card'
    {% endif %}
  </div>

But it is nor working!
Any idea?

  • When do you want to show it? Upon selection or upon submission? – leymannx Feb 20 at 19:34
  • Upon selection. – gbmapo Feb 20 at 19:35
  • I'm wondering what you are doing there with this Twig template. You shouldn't be using a custom template to build a form at all. – leymannx Feb 20 at 20:08
  • The reason why I'm doing this is that I'd like to have all my "language dependent" strings in a specific file (i.e. the Twig template) instead of embedded in the buildForm function of my form. See my other comment about your #states answer... – gbmapo Feb 20 at 22:53
0

That's a task for #states not for Twig. I think this also could be ajaxified a little bit more elegantly but using #states on some items does the job as well.

$form['payment'] = [
  '#type' => 'radios',
  '#title' => $this->t('Payment Mode'),
  '#options' => [
    'chk' => $this->t('by check'),
    'tfr' => $this->t('by bank transfer'),
    'crd' => $this->t('by card'),
  ],
  '#required' => TRUE,
];

$form['chk'] = [
  '#type' => 'item',
  '#markup' => $this->t("You chose 'Check'"),
  '#states' => [
    'visible' => [
      ':input[name="payment"]' => ['value' => 'chk'],
    ],
  ],
];

$form['tfr'] = [
  '#type' => 'item',
  '#markup' => $this->t("You chose 'Bank Transfer'"),
  '#states' => [
    'visible' => [
      ':input[name="payment"]' => ['value' => 'tfr'],
    ],
  ],
];

$form['crd'] = [
  '#type' => 'item',
  '#markup' => $this->t("You chose 'Card'"),
  '#states' => [
    'visible' => [
      ':input[name="payment"]' => ['value' => 'crd'],
    ],
  ],
];
| improve this answer | |
  • I know that I can do that (I already did it). But I have much more text than "You chose bla bla" for each option and I'd like to have it in the Twig template in order to use all HTML possibilities there. And I want to have the least possible of these in the code of the form. So how can I have '#markup' => whatever in the form and something to replace it in Twig template? – gbmapo Feb 20 at 22:57
  • @gbmapo – This won't work in the template. It's Twig, for markup, not for listening to certain events like JavaScript can. Which is the only alternative left. Pure JavaScript. – leymannx Feb 21 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.