0

I've seen some posts on this but haven't been able to find a solution for my scenario. I've got a form that I am trying to do a check for leading and trailing spaces. The is inside a hook_form_validation_validate for Drupal using Webform Validation's API. I've used a few variations, but these are the two that seem the most logical.

foreach ($items as $key => $val) {
    // @todo: Check for scenarios where $val is an array.
    $valCheckSpace = preg_match('/^\s*/', $val);
    if ($valCheckSpace) {
      $errors[$key] = t('%item cannot have leading or trailing spaces.', array('%item' => $components[$key]['name'], '%num' => $max_length));
    }
  }

  return $errors;
}

and this one:

foreach ($items as $key => $val) {
    // @todo: Check for scenarios where $val is an array.
    $valCheckSpace = preg_match('/^\s*/g', $val);
    if ($valCheckSpace) {
      $errors[$key] = t('%item cannot have leading or trailing spaces.', array('%item' => $components[$key]['name'], '%num' => $max_length));
    }
  }

  return $errors;
}

Here's one more:

foreach ($items as $key => $val) {
    // @todo: Check for scenarios where $val is an array.
    $valCheckSpace = preg_match('/^\s/', $val);
    if ($valCheckSpace) {
      $errors[$key] = t('%item cannot have leading or trailing spaces.', array('%item' => $components[$key]['name'], '%num' => $max_length));
    }
  }

  return $errors;
}

Using VSCode and XDebug, nothing fires at those breakpoints and the entry goes through when it should throw an error.

Any insight on this would be greatly appreciated.

Here is an example of the data being inputted into the form field. Form field image with entries here

The image above shows what's being entered in. There are also trailing spaces after the entries.The expected results are to throw an error to let the user know that leading or trailing spaces are not allowed.

3
  • Would you mind adding some sample input data and your expected match? Commented Oct 7, 2020 at 22:43
  • I've added an image of what is being entered and an explanation of what I'd like the result to be. The entries are simply numerical entries with leading spaces and trailing spaces. An error should be thrown to let the user know that leading or trailing spaces are not allowed. Commented Oct 8, 2020 at 1:13
  • What did you mean by "throwing an error"? For none of your examples literally throws anything. Also, are you sure that you are not using something like this: drupal.org/project/trim? Commented Oct 8, 2020 at 4:01

2 Answers 2

0

(Perl syntax) Find strings with a leading or trailing space: (^[ ].*)|(.*[ ]$)

Find strings with a leading or trailing whitespace (space or tabs): (^\W.*)|(.*\W$)

Sign up to request clarification or add additional context in comments.

Comments

0

As it turns out, Webform was executing a trim prior but the trimmed data wasn't being recognized when compared with what was already in the DB and duplicate entries were going in. The above code is actually correct. Drupal is ripping my head apart. My apologies and thanks to you all.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.