Skip to content
Snippets Groups Projects

Issue #3027240: Undefined index: #parents FormState.php (11.x)

Open Issue #3027240: Undefined index: #parents FormState.php (11.x)
3 unresolved threads
3 unresolved threads

Issue #3027240: initial commit with changes from original MR 1300 for 9.x branch 3027240-undefined_parents

Closes #3027240

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
61
62 /**
63 * Tests a form with an #after_build that adds an element with no '#parents'.
64 */
65 public function testUndefinedParentsInAfterBuild() {
66 $form_state = new FormState();
67 $form_builder = $this->container->get('form_builder');
68 $error_message = NULL;
69 try {
70 $form_builder->submitForm($this, $form_state);
71 }
72 catch (\Throwable $e) {
73 $error_message = $e->getMessage();
74 }
75
76 $this->assertTrue(empty($error_message), "The following error occurred during the form submission: {$error_message}");
  • Comment on lines +68 to +76

    PHPUnit will report the exception nicely as a error, so something like:

    Suggested change
    68 $error_message = NULL;
    69 try {
    70 $form_builder->submitForm($this, $form_state);
    71 }
    72 catch (\Throwable $e) {
    73 $error_message = $e->getMessage();
    74 }
    75
    76 $this->assertTrue(empty($error_message), "The following error occurred during the form submission: {$error_message}");
    68 $this->assertNull($form_builder->submitForm($this, $form_state), "Form submitted without triggering an exception");

    would be simpler.

  • Please register or sign in to reply
  • 115 /**
    116 * Implements hook_form_FORM_ID_alter().
    117 */
    118 function form_test_form_form_after_build_test_alter(&$form, FormStateInterface $form_state, $form_id) {
    119 $form['#after_build'][] = '_form_test_after_build';
    120 }
    121
    122 /**
    123 * After build callback to prove !empty check on #parents is necessary.
    124 *
    125 * @see https://www.drupal.org/project/drupal/issues/3027240
    126 */
    127 function _form_test_after_build($element, FormStateInterface $form_state) {
    128 // Add a new element to the form that doesn't have #parents set.
    129 $element['after_build_item'] = ['#markup' => 'after_build_item'];
    130 return $element;
  • 1169 1169 * {@inheritdoc}
    1170 1170 */
    1171 1171 public function getError(array $element) {
    1172 if ($errors = $this->getErrors()) {
    1172 if (isset($element['#parents']) && $errors = $this->getErrors()) {
    1173 1173 $parents = [];
    1174 1174 foreach ($element['#parents'] as $parent) {
    Please register or sign in to reply
    Loading