Skip to content
Snippets Groups Projects
Verified Commit a6509130 authored by Lauri Timmanee's avatar Lauri Timmanee
Browse files

Issue #3038141 by hooroomoo, bnjmnm, lauriii, pooja saraah, uzlov, Charchil...

Issue #3038141 by hooroomoo, bnjmnm, lauriii, pooja saraah, uzlov, Charchil Khandelwal, Pancho, smustgrave, joachim: Machine name field expands after failing validation
parent 2fe6ae3d
No related branches found
No related tags found
26 merge requests!54479.5.x SF update,!5014Issue #3071143: Table Render Array Example Is Incorrect,!4868Issue #1428520: Improve menu parent link selection,!4289Issue #1344552 by marcingy, Niklas Fiekas, Ravi.J, aleevas, Eduardo Morales...,!4114Issue #2707291: Disable body-level scrolling when a dialog is open as a modal,!4100Issue #3249600: Add support for PHP 8.1 Enums as allowed values for list_* data types,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2334Issue #3228209: Add hasRole() method to AccountInterface,!2062Issue #3246454: Add weekly granularity to views date sort,!1591Issue #3199697: Add JSON:API Translation experimental module,!1484Exposed filters get values from URL when Ajax is on,!1255Issue #3238922: Refactor (if feasible) uses of the jQuery serialize function to use vanillaJS,!1162Issue #3100350: Unable to save '/' root path alias,!1105Issue #3025039: New non translatable field on translatable content throws error,!1073issue #3191727: Focus states on mobile second level navigation items fixed,!925Issue #2339235: Remove taxonomy hard dependency on node module,!877Issue #2708101: Default value for link text is not saved,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links,!844Resolve #3036010 "Updaters",!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493,!485Sets the autocomplete attribute for username/password input field on login form.,!30Issue #3182188: Updates composer usage to point at ./vendor/bin/composer,!23Issue #2879087: Use comment access handler instead of hardcoding permissions
......@@ -47,6 +47,11 @@
function clickEditHandler(e) {
const data = e.data;
data.$wrapper.removeClass('hidden');
if (data.$target.attr('data-machine-name-require-when-visible')) {
data.$target
.attr('required', true)
.removeAttr('data-machine-name-require-when-visible');
}
data.$target.trigger('focus');
data.$suffix.hide();
data.$source.off('.machineName');
......@@ -110,14 +115,25 @@
) {
return;
}
// Skip processing upon a form validation error on the machine name.
if ($target.hasClass('error')) {
// Skip processing upon a form validation error on a non-empty
// machine name.
if (
$target.hasClass('error') &&
$target[0].value &&
$target[0].value.trim().length
) {
return;
}
// Figure out the maximum length for the machine name.
options.maxlength = $target.attr('maxlength');
// Hide the form item container of the machine name form element.
$wrapper.addClass('hidden');
if ($target.attr('required')) {
$target
.removeAttr('required')
.attr('data-machine-name-require-when-visible');
}
// Initial machine name from the target field default value.
const machine = $target[0].value;
// Append the machine name preview to the source field.
......@@ -176,10 +192,6 @@
// Initialize machine name preview.
.trigger('formUpdated.machineName');
}
// Add a listener for an invalid event on the machine name input
// to show its container and focus it.
$target.on('invalid', eventData, clickEditHandler);
});
},
......
......@@ -33,6 +33,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => 'Machine name 1',
'#description' => 'A machine name.',
'#machine_name' => [
'exists' => [$this, 'load'],
'source' => ['machine_name_1_label'],
],
];
......@@ -45,6 +46,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => 'Machine name 2',
'#description' => 'Another machine name.',
'#machine_name' => [
'exists' => [$this, 'load'],
'source' => ['machine_name_2_label'],
],
];
......@@ -58,6 +60,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => 'Machine name 3',
'#description' => 'Another machine name.',
'#machine_name' => [
'exists' => [$this, 'load'],
'source' => ['machine_name_3_label'],
],
];
......@@ -75,4 +78,16 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setResponse(new JsonResponse($form_state->getValues()));
}
/**
* Loading stub for machine name.
*
* @param $machine_name
* The machine name.
*
* @return bool
*/
public function load($machine_name) {
return str_contains($machine_name, 'duplicate');
}
}
......@@ -120,6 +120,51 @@ public function testMachineName() {
// Validate if the element contains the correct value.
$this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
// Test that machine name generation still occurs after an HTML 5
// validation failure.
$this->drupalGet('form-test/machine-name');
$this->assertSession()->buttonExists('Submit')->press();
// Assert all fields are initialized correctly.
$this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized');
$this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
$this->assertNotEmpty($machine_name_3_value, 'Machine name field 3 must be initialized');
// Assert that a machine name based on a default value is initialized.
$this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"');
// Test each value for conversion to a machine name.
foreach ($test_values as $test_info) {
// Set the value for the field, triggering the machine name update.
$title_1->setValue($test_info['input']);
// Wait the set timeout for fetching the machine name.
$this->assertJsCondition('jQuery("#edit-machine-name-1-label-machine-name-suffix .machine-name-value").html() == "' . $test_info['expected'] . '"');
// Validate the generated machine name.
$this->assertEquals($test_info['expected'], $machine_name_1_value->getHtml(), $test_info['message']);
// Validate the second machine name field is empty.
$this->assertEmpty($machine_name_2_value->getHtml(), 'The second machine name field should still be empty');
}
// Validate the machine name field is hidden. Elements are visually hidden
// using positioning, isVisible() will therefore not work.
$this->assertTrue($machine_name_1_wrapper->hasClass('hidden'), 'The ID field must not be visible');
$this->assertTrue($machine_name_2_wrapper->hasClass('hidden'), 'The ID field must not be visible');
// Test switching back to the manual editing mode by clicking the edit link.
$button_1->click();
// Validate the visibility of the machine name field.
$this->assertFalse($machine_name_1_wrapper->hasClass('hidden'), 'The ID field must now be visible');
// Validate the visibility of the second machine name field.
$this->assertTrue($machine_name_2_wrapper->hasClass('hidden'), 'The ID field must not be visible');
// Validate if the element contains the correct value.
$this->assertEquals($test_values[1]['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
$assert = $this->assertSession();
$this->drupalGet('/form-test/form-test-machine-name-validation');
......@@ -132,7 +177,16 @@ public function testMachineName() {
// Test a successful submit after using AJAX.
$assert->fieldExists('Name')->setValue('test 1');
$assert->fieldExists('id')->setValue('test_1');
$machine_name_value = $page->find('css', '#edit-name-machine-name-suffix .machine-name-value');
$this->assertNotEmpty($machine_name_value, 'Machine name field must be initialized');
$this->assertJsCondition('jQuery("#edit-name-machine-name-suffix .machine-name-value").html() == "' . 'test_1' . '"');
// Ensure that machine name generation still occurs after a non-HTML 5
// validation failure.
$this->assertEquals('test_1', $machine_name_value->getHtml(), $test_values[1]['message']);
$machine_name_wrapper = $page->find('css', '#edit-id')->getParent();
// Machine name field should not expand after failing validation.
$this->assertTrue($machine_name_wrapper->hasClass('hidden'), 'The ID field must not be visible');
$assert->selectExists('snack')->selectOption('apple');
$assert->assertWaitOnAjaxRequest();
$assert->buttonExists('Save')->press();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment