Answers.Body: <p>$child['#group'] is an array, you shouldn't use explode on array at line 152</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-28T12:30:33.910Z
Answers.Text: But I am not? That code is in the Drupal/Core/Form/FormErrorHandler.php and it works fine in a non CiviCRM integrated form. This code appears to bubble up all errors on any form?  I am not aware I have done anything special in my form when adding these contacts?
Answers.UserDisplayName: ChumKui
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-28T13:59:30.217Z
Answers.Text: Can you check your webform xml config, something must be wrong
Answers.UserDisplayName: Pradeep Nayak
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-29T09:08:55.263Z
Answers.Text: Thanks Pradeep - the issue cleared itself after I switch the order of the Handler.  When you advise check your 'webform xml config' I would not know where to start or what I'm looking for.  This was an existing form that has been working, all I did was add this simple validation handler.  I'll see if I can reproduce.
Answers.UserDisplayName: ChumKui
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-28T12:00:57.677Z
Answers.LastActivityDate: 2024-05-28T12:00:57.677Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>I have a Drupal 10 Webform integrated with CiviCRM (5.73.1) and thought I'd like to add some additional validation using the newer Webform Handler technique.</p>
  <p>Images are uploaded and I have created a very simple custom module that looks at whether images actually have been uploaded and if so, that an 'authorised' radio is also checked to 'Yes':</p>
  <pre><code>class WebformImageHandler extends WebformHandlerBase
  {
  
      /**
       * {@inheritdoc}
       */
      public function validateForm(array &amp;$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission)
      {
          $this-&gt;validateAuthorised($form_state);
      }
  
  
  
      /**
       * Check to see if there are any images uploaded.  If there are no images and authorised is not set to 'Yes'
       * display a message to the user advising them that there has to be images present.  If there are images present
       * and authorised is not set to 'Yes' display a message to the user advising them that they must authorise the form
       *
       * @param \Drupal\Core\Form\FormStateInterface $form_state
       *   The form state.
       */
      protected function validateAuthorised(FormStateInterface $form_state)
      {
          $values = $form_state-&gt;getValues();
          $images = $values['my_photo'];
          $authorised = $values['civicrm_1_activity_1_cg55_custom_530'];
          //$images = $values['image'];
          //$authorised = $values['authorised'];
  
  
          if (!empty($images) &amp;&amp; $authorised !== 'Yes') {
              $form_state-&gt;setErrorByName('Please review the Authorised field', $this-&gt;t(&quot;You must set authorise to 'Yes' if you are submitting one or more pictures.&quot;));
          }
          if (empty($images) &amp;&amp; $authorised !== 'No') {
              $form_state-&gt;setErrorByName('Please review the Authorised field', $this-&gt;t(&quot;You must set authorise to 'No' if you are not uploading imaged.&quot;));
          }
      }
  </code></pre>
  <p>As I had never done a Webform Handler before, I first tested that it worked for a simple two field form without any integration with CiviCRM - it worked as expected. I then added the new handler to my CiviCRM form but it crashes on submission during testing with this log error message:</p>
  <pre><code>TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() (line 152 of /var/www/html/web/core/lib/Drupal/Core/Form/FormErrorHandler.php).
  </code></pre>
  <p>Looking through the debugger, I can see that the validation itself works fine and the correct <code>$form_state-&gt;setErrorByName()</code> is fired. It seems to fail due to the presence of a CiviCRM organisation field? There are a total of 3 contacts, plus an activity.  Here is where I got to on the debugger:</p>
  <p><a href="https://i.sstatic.net/xpxTMtiI.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xpxTMtiI.png" alt="enter image description here" /></a></p>
  <p>I'm well out of my depth here, so asking if this is a problem with the way I have approached it or a possible bug?</p>
  <p>*** UPDATE ***
  This is still an issue but I have narrowed it down and can reproduce so I am pretty sure it is a bug - it is definitively to do with the Contact filter on the CiviCRM contact field.</p>
  <p>There are two additional contacts on my form that are prepopulated with Contact IDs.  These are 'locked' by way of the Filter:</p>
  <p><a href="https://i.sstatic.net/7dawk7eK.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7dawk7eK.png" alt="Contact Filters" /></a></p>
  <p>If this Filter is present, the form fails, removing the Filter and the validation works as intended.</p>
  <p>To reproduce it create a simple form with just a single CiviCRM contact and nothing else - just ensure you set a filter on it to any group (even 'none').  Add my validation handler and it will fail.</p>
  <p>Here is the code: <a href="https://gist.github.com/chumkui/467d794bb28dd7b7ea9f74f8e176ff16" rel="nofollow noreferrer">Gist of form and Webform Handler</a></p>
  <p>So forgive me for my lack of knowledge, but this is a bug to report where exactly?  Is it the Webform-CiviCRM module?</p>
  <p><em><strong>UPDATE 2</strong></em>
  This is reproduceable without a handler - Create a form that has a single image upload field, integrate with CiviCRM and ensure the contact filter is set to anything (even 'None').  The form will crash.</p>
  <p>Its a reported issue... <a href="https://www.drupal.org/project/webform_civicrm/issues/3254254" rel="nofollow noreferrer">https://www.drupal.org/project/webform_civicrm/issues/3254254</a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-28T19:43:43.380Z
Comments.Text: does the order of handlers matter?
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-29T08:55:23.780Z
Comments.Text: Peter - you are a legend.  That is what the issue was, well sort of... I moved the Handler before the CiviCRM handler and it worked.  Curious I moved it to other positions (there are 10 including this one that send various emails) and it was fine.  So finally I moved it to the end again and it worked... I'm not a developer so just getting to this was tough for me.  If I can reproduce it by removing and adding the handler I'll take a look at the delta in the source.  Thanks again.
Comments.UserDisplayName: ChumKui
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-29T11:20:19.357Z
Comments.Text: OK - I have tried to reproduce this issue and simply cannot.  Possibly some sort of a caching issue? I'll add an answer that if you have an issue like this, to try in the first instance to swap the Handler ordering.
Comments.UserDisplayName: ChumKui
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-29T16:32:19.700Z
Comments.Text: OK - this is still an issue.  I thought the moving of the handlers around did it and should have been curious when it worked in the original position... I had forgotten i'd commented some code out the night before to get it to submit.  So, it is reproduceable locally and I'm still no nearer so I have deleted my 'answer'.
Comments.UserDisplayName: ChumKui
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-29T17:53:39.320Z
Comments.Text: Have now reproduced and updated my original question
Comments.UserDisplayName: ChumKui
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-30T08:47:45.410Z
Comments.Text: This is already an issue - just found it... 
   https://www.drupal.org/project/webform_civicrm/issues/3254254
Comments.UserDisplayName: ChumKui
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-30T20:12:10.570Z
Comments.Text: Did you try the Patch from https://www.drupal.org/project/webform_civicrm/issues/3254254#comment-15343756. if not then pls do if you know how to, and provide some feedback on it so we can get it merged
Comments.UserDisplayName: petednz - fuzion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-31T12:39:27.280Z
Comments.Text: No, I have not tried that patch - not exaclty my wheelhouse, but I have got to learn more of this stuff so will give it a go!
Comments.UserDisplayName: ChumKui
Tags: <webform-civicrm>
Title: Custom Webform Handler crashes with CiviCRM contacts present
ThreadText: # Custom Webform Handler crashes with CiviCRM contacts present
  
  (Asked by: username_0 on 2024-05-28)
  
  I have a Drupal 10 Webform integrated with CiviCRM (5.73.1) and thought I'd like to add some additional validation using the newer Webform Handler technique.
  
  Images are uploaded and I have created a very simple custom module that looks at whether images actually have been uploaded and if so, that an 'authorised' radio is also checked to 'Yes':
  
  ```
  class WebformImageHandler extends WebformHandlerBase
  {
  
      /**
       * {@inheritdoc}
       */
      public function validateForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission)
      {
          $this->validateAuthorised($form_state);
      }
  
      /**
       * Check to see if there are any images uploaded.  If there are no images and authorised is not set to 'Yes'
       * display a message to the user advising them that there has to be images present.  If there are images present
       * and authorised is not set to 'Yes' display a message to the user advising them that they must authorise the form
       *
       * @param \Drupal\Core\Form\FormStateInterface $form_state
       *   The form state.
       */
      protected function validateAuthorised(FormStateInterface $form_state)
      {
          $values = $form_state->getValues();
          $images = $values['my_photo'];
          $authorised = $values['civicrm_1_activity_1_cg55_custom_530'];
          //$images = $values['image'];
          //$authorised = $values['authorised'];
  
          if (!empty($images) && $authorised !== 'Yes') {
              $form_state->setErrorByName('Please review the Authorised field', $this->t("You must set authorise to 'Yes' if you are submitting one or more pictures."));
          }
          if (empty($images) && $authorised !== 'No') {
              $form_state->setErrorByName('Please review the Authorised field', $this->t("You must set authorise to 'No' if you are not uploading imaged."));
          }
      }
  
  ```
  
  As I had never done a Webform Handler before, I first tested that it worked for a simple two field form without any integration with CiviCRM - it worked as expected. I then added the new handler to my CiviCRM form but it crashes on submission during testing with this log error message:
  
  ```
  TypeError: explode(): Argument #2 ($string) must be of type string, array given in explode() (line 152 of /var/www/html/web/core/lib/Drupal/Core/Form/FormErrorHandler.php).
  
  ```
  
  Looking through the debugger, I can see that the validation itself works fine and the correct `$form_state->setErrorByName()` is fired. It seems to fail due to the presence of a CiviCRM organisation field? There are a total of 3 contacts, plus an activity. Here is where I got to on the debugger:
  
  [![enter image description here](https://i.sstatic.net/xpxTMtiI.png)](https://i.sstatic.net/xpxTMtiI.png)
  
  I'm well out of my depth here, so asking if this is a problem with the way I have approached it or a possible bug?
  
  \*\*\* UPDATE \*\*\*
  This is still an issue but I have narrowed it down and can reproduce so I am pretty sure it is a bug - it is definitively to do with the Contact filter on the CiviCRM contact field.
  
  There are two additional contacts on my form that are prepopulated with Contact IDs. These are 'locked' by way of the Filter:
  
  [![Contact Filters](https://i.sstatic.net/7dawk7eK.png)](https://i.sstatic.net/7dawk7eK.png)
  
  If this Filter is present, the form fails, removing the Filter and the validation works as intended.
  
  To reproduce it create a simple form with just a single CiviCRM contact and nothing else - just ensure you set a filter on it to any group (even 'none'). Add my validation handler and it will fail.
  
  Here is the code: [Gist of form and Webform Handler](https://gist.github.com/username_0/467d794bb28dd7b7ea9f74f8e176ff16)
  
  So forgive me for my lack of knowledge, but this is a bug to report where exactly? Is it the Webform-CiviCRM module?
  
  ***UPDATE 2***
  This is reproduceable without a handler - Create a form that has a single image upload field, integrate with CiviCRM and ensure the contact filter is set to anything (even 'None'). The form will crash.
  
  Its a reported issue... <https://www.drupal.org/project/webform_civicrm/issues/3254254>
  
  **Tags:** webform-civicrm
  
  > **Comments:**
  > **username_1**: does the order of handlers matter?  
  > **username_0**: Peter - you are a legend.  That is what the issue was, well sort of... I moved the Handler before the CiviCRM handler and it worked.  Curious I moved it to other positions (there are 10 including this one that send various emails) and it was fine.  So finally I moved it to the end again and it worked... I'm not a developer so just getting to this was tough for me.  If I can reproduce it by removing and adding the handler I'll take a look at the delta in the source.  Thanks again.  
  > **username_0**: OK - I have tried to reproduce this issue and simply cannot.  Possibly some sort of a caching issue? I'll add an answer that if you have an issue like this, to try in the first instance to swap the Handler ordering.  
  > **username_0**: OK - this is still an issue.  I thought the moving of the handlers around did it and should have been curious when it worked in the original position... I had forgotten i'd commented some code out the night before to get it to submit.  So, it is reproduceable locally and I'm still no nearer so I have deleted my 'answer'.  
  > **username_0**: Have now reproduced and updated my original question  
  > **username_0**: This is already an issue - just found it...   https://www.drupal.org/project/webform_civicrm/issues/3254254  
  > **username_1**: Did you try the Patch from https://www.drupal.org/project/webform_civicrm/issues/3254254#comment-15343756. if not then pls do if you know how to, and provide some feedback on it so we can get it merged  
  > **username_0**: No, I have not tried that patch - not exaclty my wheelhouse, but I have got to learn more of this stuff so will give it a go!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-05-28. Score: 0
  
  $child['#group'] is an array, you shouldn't use explode on array at line 152
  
  > **Comments:**
  > **username_0**: But I am not? That code is in the Drupal/Core/Form/FormErrorHandler.php and it works fine in a non CiviCRM integrated form. This code appears to bubble up all errors on any form?  I am not aware I have done anything special in my form when adding these contacts?  
  > **username_2**: Can you check your webform xml config, something must be wrong  
  > **username_0**: Thanks username_2 - the issue cleared itself after I switch the order of the Handler.  When you advise check your 'webform xml config' I would not know where to start or what I'm looking for.  This was an existing form that has been working, all I did was add this simple validation handler.  I'll see if I can reproduce.  
  
  ---
  
  