Commit aeec851c authored by catch's avatar catch
Browse files

fix: #3061838 Email element does not have default #ajax event

By: mglaman
By: smustgrave
By: megachriz
By: kart.vajakas
By: katzilla
By: vijaycs85
By: pguillard
By: parth_sinha
By: xjm
By: kazuko.murata
By: otofu
By: a_ramos
By: silverham
By: dokumori
By: johnalbin
By: gengo_k
By: rduterte
(cherry picked from commit 27e9d0c2)
parent 261c3d78
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -30530,12 +30530,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\ajax_forms_test\\\\Form\\\\AjaxFormsTestSimpleForm\\:\\:textfieldCallback\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestSimpleForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\ajax_forms_test\\\\Form\\\\AjaxFormsTestValidationForm\\:\\:submitForm\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+1 −0
Original line number Diff line number Diff line
@@ -315,6 +315,7 @@ public static function preRenderAjaxForm($element) {
        case 'number':
        case 'tel':
        case 'textarea':
        case 'email':
          $element['#ajax']['event'] = 'blur';
          break;

+41 −5
Original line number Diff line number Diff line
@@ -111,6 +111,20 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#title' => $this->t('Another AJAX checkbox in a nested group'),
    ];

    $form['email_group'] = [
      '#type' => 'details',
      '#title' => $this->t('Email test group'),
      '#open' => TRUE,
    ];

    $form['email_group']['email_field_1'] = [
      '#type' => 'email',
      '#title' => 'Email 1',
      '#ajax' => [
        'callback' => [static::class, 'emailFieldCallback'],
      ],
    ];

    $form['textfield_focus_tests'] = [
      '#type' => 'details',
      '#title' => $this->t('Test group 2'),
@@ -120,14 +134,14 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#type' => 'textfield',
      '#title' => 'Textfield 1',
      '#ajax' => [
        'callback' => [static::class, 'textfieldCallback'],
        'callback' => [static::class, 'textFieldCallback'],
      ],
    ];
    $form['textfield_focus_tests']['textfield_2'] = [
      '#type' => 'textfield',
      '#title' => 'Textfield 2',
      '#ajax' => [
        'callback' => [static::class, 'textfieldCallback'],
        'callback' => [static::class, 'textFieldCallback'],
        'event' => 'change',
        'refocus-blur' => FALSE,
      ],
@@ -136,7 +150,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
      '#type' => 'textfield',
      '#title' => 'Textfield 3',
      '#ajax' => [
        'callback' => [static::class, 'textfieldCallback'],
        'callback' => [static::class, 'textFieldCallback'],
        'event' => 'change',
      ],
    ];
@@ -144,8 +158,30 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  public static function textfieldCallback($form) {
    return $form;
  /**
   * AJAX callback for textfield.
   *
   * @param array $form
   *   The form array.
   *
   * @return array
   *   The form element to return via AJAX.
   */
  public static function textFieldCallback(array $form): array {
    return $form['textfield_focus_tests'];
  }

  /**
   * AJAX callback for email field.
   *
   * @param array $form
   *   The form array.
   *
   * @return array
   *   The form element to return via AJAX.
   */
  public static function emailFieldCallback(array $form): array {
    return $form['email_group'];
  }

  /**
+8 −0
Original line number Diff line number Diff line
@@ -359,6 +359,14 @@ public function testAjaxFocus(): void {
    $this->assertSession()->assertWaitOnAjaxRequest();
    $has_focus_id = $this->getSession()->evaluateScript('document.activeElement.id');
    $this->assertEquals('edit-textfield-3', $has_focus_id);

    $this->assertNotNull($email_field1 = $this->assertSession()->elementExists('css', '#edit-email-field-1'));
    // Test email field with 'blur' event listener.
    $email_field1->setValue('user@example.com');
    $email_field1->focus();
    $this->assertSession()->assertWaitOnAjaxRequest();
    $has_focus_id = $this->getSession()->evaluateScript('document.activeElement.id');
    $this->assertEquals('edit-email-field-1', $has_focus_id);
  }

}