Skip to content

Error when using multi-checkboxes #31

@oswaldimx

Description

@oswaldimx

If one uses an input of type multicheckbox in the form definition, which contains more than one entry, and a frontend-user actually selects any of those checkboxes, an error will occurr in the EmailFinisher, letting the whole form finisher workflow fail.

Reason: The value of a multicheckbox field out of the FormFramework is an array, but the original EmailFinisher class can only deal with string-like values. The array value causes an exception to be risen, breaking the form finisher workflow.

Suggestion to fix this: Overwrite method TYPO3\CMS\Form\Domain\Finisher\AbstractFinisher::substituteRuntimeReferences and add support for array values.

This would be the code to add in class Kitzberger\FormMailtext\Domain\Finishers\EmailFinisher - note the is_array check at the very bottom:

protected function substituteRuntimeReferences($needle, FormRuntime $formRuntime) {
    // neither array nor string, directly return
    if(!is_array($needle) && !is_string($needle)) {
      return $needle;
    }

    // resolve (recursively) all array items
    if(is_array($needle)) {
      $substitutedNeedle = [];
      foreach($needle as $key => $item) {
        $key = $this->substituteRuntimeReferences($key, $formRuntime);
        $item = $this->substituteRuntimeReferences($item, $formRuntime);
        $substitutedNeedle[$key] = $item;
      }
      return $substitutedNeedle;
    }

    // substitute one(!) variable in string which either could result
    // again in a string or an array representing multiple values
    if(preg_match('/^{([^}]+)}$/', $needle, $matches)) {
      return $this->resolveRuntimeReference(
        $matches[1],
        $formRuntime
      );
    }

    return preg_replace_callback(
      '/{([^}]+)}/',
      function($matches) use ($formRuntime) {
        $value = $this->resolveRuntimeReference(
          $matches[1],
          $formRuntime
        );

        // substitute each match by returning the resolved value
        if(is_array($value)) {
          return implode(' / ', $value);
        }
        return $value;
      },
      $needle
    );
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions