the browser-facing portion of osu!
at master 41 lines 1.2 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Traits; 7 8use App\Libraries\ValidationErrors; 9 10trait Validatable 11{ 12 protected $_validationErrors = null; 13 14 abstract public function validationErrorsTranslationPrefix(): string; 15 16 public function validationErrorsKeyBase(): string 17 { 18 return 'model_validation.'; 19 } 20 21 public function validationErrors(): ValidationErrors 22 { 23 if ($this->_validationErrors === null) { 24 $this->_validationErrors = new ValidationErrors( 25 $this->validationErrorsTranslationPrefix(), 26 $this->validationErrorsKeyBase() 27 ); 28 } 29 30 return $this->_validationErrors; 31 } 32 33 protected function validateFieldLength(int $limit, string $field, ?string $checkField = null): void 34 { 35 $checkField ??= $field; 36 $val = $this->$checkField; 37 if ($val !== null && mb_strlen($val) > $limit) { 38 $this->validationErrors()->add($field, 'too_long', ['limit' => $limit]); 39 } 40 } 41}