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:
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:
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