-3

I have this custom form in which there is this:
enter image description here The corresponding code:

$form['actions'] = array(
    '#type' => 'details',
    '#title' => 'Tâches associées',
    '#open' => TRUE,
);
$form['actions']['1A'] = array(
    '#type' => 'inline_template',
    '#template' => '<select name="1B"><option value="" selected="selected">1. Choisir l\'année</option><option value='.$iY1.'>'.$iY1.'</option><option value='. $iY2.'>'.$iY2.'</option></select>',
);
$form['actions']['1C'] = array(
    '#type' => 'submit',
    '#name' => '1',
    '#value' => 'Oui',
    '#submit' => array('::executeActions'),
);
$form['actions']['2A'] = array(
    '#type' => 'inline_template',
    '#template' => '<BR>2. La période de renouvellement pour l\'année '.$iRenewalPeriodYear.' est ' . $sPeriodStatus . '. Voulez-vous ' . $sPeriodAction . ' ?  ',
);
$form['actions']['2B'] = array(
    '#type' => 'submit',
    '#name' => '2',
    '#value' => 'Oui',
    '#submit' => array('::executeActions'),
);

It works fine!
Using either of the two 'OUI' buttons triggers the 'executeActions' method.

I'd like to get rid of the first 'OUI' button.
Is there a way to trigger 'executeActions' method when selecting one of the options in the dropdown list?

If not possible, how could I have the 'OUI' button on the same line than the dropdown list?

  • 1
    That’s going to be very awkward to make work - you can’t use the form API for half a form and expect to get all the benefits out of it. You should just convert the raw HTML string into its form API equivalent, and use the standard AJAX functionality – Clive Feb 1 at 15:42
  • how could I have the 'OUI' button on the same line than the dropdown list? that's just basic CSS. using float: left; OR display: inline-block;. – No Sssweat Feb 1 at 15:51
  • as @Clive mentioned, using '#type' => 'inline_template' is a bad idea, as your selection prob won't be passed to $form_state and if it's passed, it will be difficult to identify. Use '#type' => 'select' instead. – No Sssweat Feb 1 at 15:57
0

Following the advice given from @Clive and @NoSssweat, I rewritten the code.

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['suscribe'] = [
    '#type' => 'radios',
    '#title' => '',
    '#options' => [
      0 => $this->t('No'),
      1 => $this->t('Yes'),
    ],
    '#default_value' => $iWish,
    '#attributes' => [
      'onchange' => 'hasChanged(this)'
    ],
    '#validated' => TRUE,
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Submit'),
  ];

  $form['actions'] = [
    '#type' => 'details',
    '#title' => 'Tâches associées',
    '#open' => TRUE,
  ];

  if ($rpStep == 0) {
    $iY1 = (int) strftime("%Y");
    $iY2 = $iY1 + 1;

    $form['actions']['1B'] = array(
      '#type' => 'select',
      '#title' => '1. Choisir l\'année et ouvrir la période de renouvellement.',
      '#options' => array(
        "" => "--",
        $iY1 => $iY1,
        $iY2 => $iY2,
      ),
      '#default_value' => $rpYear,
    );
  }
  else {
    $form['actions']['1A'] = array(
      '#markup' => '1. La période de renouvellement a été ouverte pour ' . $rpYear . '.',
    );
    $form['actions']['4B'] = array(
      '#type' => 'radios',
      '#title' => 'Voulez-vous fermer la période de renouvellement ?',
      '#options' => [
        0 => $this->t('No'),
        1 => $this->t('Yes'),
      ],
      '#default_value' => 0,
    );
  }

  $form['actions']['99'] = array(
    '#type' => 'submit',
    '#name' => '99',
    '#value' => $this->t('Submit'),
    '#submit' => array('::executeActions'),
  );

  return $form;
}

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.