Dec 16, 2019 in Drupal 8, Snippets

In drupal 8, each entity will get a UUID assigned automatically. With this snippet you can check if it is a valid one. It makes us of a component in drupal.

For security reasons, for example when prefilling forms I recommend to check if a given string or parameter is a valid uuid.

use Drupal\Component\Uuid\Uuid;

$uuid_to_validate = '550e8400-e29b-41d4-a716-446655440000';
$IsValidUuid = Uuid::isValid($uuid_to_validate);
if($isValidUuid) {
  // This is a valid uuid