Loading core/lib/Drupal/Core/Recipe/RecipeInputFormTrait.php +5 −2 Original line number Diff line number Diff line Loading @@ -76,9 +76,12 @@ public function collectValue(string $name, DataDefinitionInterface $definition, $element += [ '#description' => $definition->getDescription(), '#default_value' => $default_value, '#type' => 'value', ]; // Recipe inputs are always required. $element['#required'] = TRUE; // Recipe inputs are required by default, unless they are single // checkboxes, in which case the `#required` behavior doesn't make // a lot of sense because it forces the user to check the box. $element['#required'] ??= ($element['#type'] !== 'checkbox'); NestedArray::setValue($this->form, explode('.', $name, 2), $element); // Always return the input elements as a tree. Loading core/modules/system/tests/src/Functional/Form/RecipeFormInputTest.php +6 −3 Original line number Diff line number Diff line Loading @@ -29,15 +29,18 @@ public function testRecipeInputViaForm(): void { $this->drupalGet('/form-test/recipe-input'); $assert_session = $this->assertSession(); // There should only be one nested input element on the page: the one // There should only be two nested input elements on the page: the two // defined by the input_test recipe. $assert_session->elementsCount('css', 'input[name*="["]', 1); $assert_session->elementsCount('css', 'input[name*="["]', 2); // The default value and description should be visible. $assert_session->fieldValueEquals('input_test[owner]', 'Dries Buytaert'); $assert_session->pageTextContains('The name of the site owner.'); // All recipe inputs are required. $this->assertSame('checkbox', $assert_session->fieldExists('Allow mischief')->getAttribute('type')); // All recipe inputs are required, except for checkboxes, for which that // behavior makes no sense. $this->submitForm(['input_test[owner]' => ''], 'Apply recipe'); $assert_session->statusMessageContains("Site owner's name field is required.", 'error'); $assert_session->statusMessageNotContains('Allow mischief field is required.', 'error'); // All inputs should be validated with their own constraints. $this->submitForm(['input_test[owner]' => 'Hacker Joe'], 'Apply recipe'); $assert_session->statusMessageContains("I don't think you should be owning sites.", 'error'); Loading core/tests/fixtures/recipes/input_test/recipe.yml +9 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,15 @@ input: default: source: value value: 'Dries Buytaert' break_stuff: data_type: boolean description: 'Should mischief be allowed on this site? Not recommended.' form: '#type': checkbox '#title': 'Allow mischief' default: source: value value: false config: actions: system.site: Loading Loading
core/lib/Drupal/Core/Recipe/RecipeInputFormTrait.php +5 −2 Original line number Diff line number Diff line Loading @@ -76,9 +76,12 @@ public function collectValue(string $name, DataDefinitionInterface $definition, $element += [ '#description' => $definition->getDescription(), '#default_value' => $default_value, '#type' => 'value', ]; // Recipe inputs are always required. $element['#required'] = TRUE; // Recipe inputs are required by default, unless they are single // checkboxes, in which case the `#required` behavior doesn't make // a lot of sense because it forces the user to check the box. $element['#required'] ??= ($element['#type'] !== 'checkbox'); NestedArray::setValue($this->form, explode('.', $name, 2), $element); // Always return the input elements as a tree. Loading
core/modules/system/tests/src/Functional/Form/RecipeFormInputTest.php +6 −3 Original line number Diff line number Diff line Loading @@ -29,15 +29,18 @@ public function testRecipeInputViaForm(): void { $this->drupalGet('/form-test/recipe-input'); $assert_session = $this->assertSession(); // There should only be one nested input element on the page: the one // There should only be two nested input elements on the page: the two // defined by the input_test recipe. $assert_session->elementsCount('css', 'input[name*="["]', 1); $assert_session->elementsCount('css', 'input[name*="["]', 2); // The default value and description should be visible. $assert_session->fieldValueEquals('input_test[owner]', 'Dries Buytaert'); $assert_session->pageTextContains('The name of the site owner.'); // All recipe inputs are required. $this->assertSame('checkbox', $assert_session->fieldExists('Allow mischief')->getAttribute('type')); // All recipe inputs are required, except for checkboxes, for which that // behavior makes no sense. $this->submitForm(['input_test[owner]' => ''], 'Apply recipe'); $assert_session->statusMessageContains("Site owner's name field is required.", 'error'); $assert_session->statusMessageNotContains('Allow mischief field is required.', 'error'); // All inputs should be validated with their own constraints. $this->submitForm(['input_test[owner]' => 'Hacker Joe'], 'Apply recipe'); $assert_session->statusMessageContains("I don't think you should be owning sites.", 'error'); Loading
core/tests/fixtures/recipes/input_test/recipe.yml +9 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,15 @@ input: default: source: value value: 'Dries Buytaert' break_stuff: data_type: boolean description: 'Should mischief be allowed on this site? Not recommended.' form: '#type': checkbox '#title': 'Allow mischief' default: source: value value: false config: actions: system.site: Loading