77use Illuminate \Validation \Rule ;
88use Illuminate \Container \Container ;
99use Illuminate \Contracts \Validation \Factory ;
10+ use Illuminate \Validation \ValidationException ;
1011use Illuminate \Database \Eloquent \Factories \HasFactory ;
1112use Pterodactyl \Exceptions \Model \DataValidationException ;
1213use Illuminate \Database \Eloquent \Model as IlluminateModel ;
@@ -30,13 +31,6 @@ abstract class Model extends IlluminateModel
3031 */
3132 protected $ skipValidation = false ;
3233
33- /**
34- * The validator instance used by this model.
35- *
36- * @var \Illuminate\Validation\Validator
37- */
38- protected $ validator ;
39-
4034 /**
4135 * @var \Illuminate\Contracts\Validation\Factory
4236 */
@@ -60,8 +54,10 @@ protected static function boot()
6054 static ::$ validatorFactory = Container::getInstance ()->make (Factory::class);
6155
6256 static ::saving (function (Model $ model ) {
63- if (!$ model ->validate ()) {
64- throw new DataValidationException ($ model ->getValidator ());
57+ try {
58+ $ model ->validate ();
59+ } catch (ValidationException $ exception ) {
60+ throw new DataValidationException ($ exception ->validator );
6561 }
6662
6763 return true ;
@@ -101,14 +97,9 @@ public function skipValidation()
10197 */
10298 public function getValidator ()
10399 {
104- $ rules = $ this ->getKey () ? static ::getRulesForUpdate ($ this ) : static ::getRules ();
100+ $ rules = $ this ->exists ? static ::getRulesForUpdate ($ this ) : static ::getRules ();
105101
106- return $ this ->validator ?: $ this ->validator = static ::$ validatorFactory ->make (
107- [],
108- $ rules ,
109- [],
110- []
111- );
102+ return static ::$ validatorFactory ->make ([], $ rules , [], []);
112103 }
113104
114105 /**
@@ -139,14 +130,14 @@ public static function getRulesForField(string $field): array
139130 * Returns the rules associated with the model, specifically for updating the given model
140131 * rather than just creating it.
141132 *
142- * @param \Illuminate\Database\Eloquent\Model|int|string $id
133+ * @param \Illuminate\Database\Eloquent\Model|int|string $model
143134 *
144135 * @return array
145136 */
146- public static function getRulesForUpdate ($ id , string $ primaryKey = 'id ' )
137+ public static function getRulesForUpdate ($ model , string $ column = 'id ' )
147138 {
148- if ($ id instanceof Model) {
149- [$ primaryKey , $ id ] = [$ id -> getKeyName (), $ id -> getKey ()];
139+ if ($ model instanceof Model) {
140+ [$ id , $ column ] = [$ model -> getKey (), $ model -> getKeyName ()];
150141 }
151142
152143 $ rules = static ::getRules ();
@@ -163,7 +154,7 @@ public static function getRulesForUpdate($id, string $primaryKey = 'id')
163154 [, $ args ] = explode (': ' , $ datum );
164155 $ args = explode (', ' , $ args );
165156
166- $ datum = Rule::unique ($ args [0 ], $ args [1 ] ?? $ key )->ignore ($ id , $ primaryKey )-> __toString ( );
157+ $ datum = Rule::unique ($ args [0 ], $ args [1 ] ?? $ key )->ignore ($ id ?? $ model , $ column );
167158 }
168159 }
169160
@@ -172,24 +163,27 @@ public static function getRulesForUpdate($id, string $primaryKey = 'id')
172163
173164 /**
174165 * Determines if the model is in a valid state or not.
175- *
176- * @return bool
177166 */
178- public function validate ()
167+ public function validate (): void
179168 {
180169 if ($ this ->skipValidation ) {
181- return true ;
170+ return ;
182171 }
183172
184- return $ this ->getValidator ()->setData (
173+ $ validator = $ this ->getValidator ();
174+ $ validator ->setData (
185175 // Trying to do self::toArray() here will leave out keys based on the whitelist/blacklist
186176 // for that model. Doing this will return all of the attributes in a format that can
187177 // properly be validated.
188178 $ this ->addCastAttributesToArray (
189179 $ this ->getAttributes (),
190180 $ this ->getMutatedAttributes ()
191181 )
192- )->passes ();
182+ );
183+
184+ if (!$ validator ->passes ()) {
185+ throw new ValidationException ($ validator );
186+ }
193187 }
194188
195189 /**
0 commit comments