Validate a UUID in drupal 8 (check if uuid is valid)

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
}

 

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!