0

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

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

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

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

8
  • does the order of handlers matter?
    – petednz - fuzion
    Commented May 28 at 19:43
  • 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.
    – ChumKui
    Commented May 29 at 8:55
  • 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.
    – ChumKui
    Commented May 29 at 11:20
  • 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'.
    – ChumKui
    Commented May 29 at 16:32
  • 1
    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!
    – ChumKui
    Commented May 31 at 12:39

1 Answer 1

0

$child['#group'] is an array, you shouldn't use explode on array at line 152

3
  • 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?
    – ChumKui
    Commented May 28 at 12:30
  • Can you check your webform xml config, something must be wrong Commented May 28 at 13:59
  • 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.
    – ChumKui
    Commented May 29 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.