diff --git a/gatherling/Exceptions/ValidationException.php b/gatherling/Exceptions/ValidationException.php index c3b34ae63..4552be600 100644 --- a/gatherling/Exceptions/ValidationException.php +++ b/gatherling/Exceptions/ValidationException.php @@ -8,6 +8,6 @@ class ValidationException extends BadRequestException { public function getUserMessage(): string { - return 'The request was invalid'; + return $this->getMessage(); } } diff --git a/gatherling/Models/Event.php b/gatherling/Models/Event.php index a022f8f22..98a23dc66 100644 --- a/gatherling/Models/Event.php +++ b/gatherling/Models/Event.php @@ -267,6 +267,42 @@ public static function createEvent( return $event; } + private function validate(): void + { + try { + strtotime($this->start); + } catch (DatetimeException $e) { + throw new ValidationException("Invalid start date {$this->start}"); + } + try { + db()->int('SELECT 1 FROM formats WHERE name = :name', ['name' => $this->format]); + } catch (DatabaseException $e) { + throw new ValidationException("Invalid format {$this->format}"); + } + try { + db()->int('SELECT 1 FROM players WHERE name = :name', ['name' => $this->host]); + } catch (DatabaseException $e) { + throw new ValidationException("Invalid host {$this->host}"); + } + if ($this->cohost !== null) { + try { + db()->int('SELECT 1 FROM players WHERE name = :name', ['name' => $this->cohost]); + } catch (DatabaseException $e) { + throw new ValidationException("Invalid cohost {$this->cohost}"); + } + } + try { + db()->int('SELECT 1 FROM series WHERE name = :name', ['name' => $this->series]); + } catch (DatabaseException $e) { + throw new ValidationException("Invalid series {$this->series}"); + } + try { + db()->int('SELECT 1 FROM clients WHERE id = :id', ['id' => $this->client]); + } catch (DatabaseException $e) { + throw new ValidationException("Invalid client {$this->client}"); + } + } + public function save(): void { if ($this->cohost == '') { @@ -279,10 +315,9 @@ public function save(): void $this->active = 0; } + $this->validate(); + if ($this->new) { - if (!$this->series) { - throw new ValidationException("Series is required for a new event"); - } $sql = ' INSERT INTO events (name, start, format, host, cohost, kvalue, number, season, series, threadurl, reporturl, metaurl, prereg_allowed, finalized, player_reportable, prereg_cap, player_editdecks,