Hero

Laravel Validatorの実装を追ってみたよ。

Date: 2021/08/15 13:04

Category: none

そもそも

PHPのWebFrameworkであるLaravelには、様々な便利な機能がある。
FormRequestもそのひとつである。
これを用いることで、フォームから送信された値にvalidationをかけることができる。
rules()にバリデーションルールを、messages()にどんなバリデーションエラーメッセージを表示するかを指定することができる。

1 /** 2 * Get the validation rules that apply to the request. 3 * 4 * @return array 5 */ 6 public function rules() 7 { 8 return [ 9 'contact_form_name' => ['required', 'string:255'], 10 'contact_form_email' => ['required', 'email:rfc'], 11 'contact_form_phone_number' => ['nullable', 'digits_between:10,11'], 12 'contact_form_options' => ['required'], 13 'contact_form_inquire_content' => ['required'], 14 'g-recaptcha-response' => ['required','captcha'], 15 ]; 16 } 17 18 /** 19 * Validate Message 20 * @return array 21 */ 22 public function messages(){ 23 return [ 24 'contact_form_name.required' => '名前を入力してください', 25 'contact_form_name.string' => '255文字以内で入力してください', 26 'contact_form_email.required' => 'メールアドレスを入力してください', 27 'contact_form_phone_number.digits_between' => '10桁もしくは11桁で入力してください', 28 'contact_form_options.required' => '問い合わせ項目を入力してください', 29 'contact_form_inquire_content.required' => '問い合わせ内容を入力してください', 30 'g-recaptcha-response.required' => 'チェックしてください', 31 ]; 32 } 33

そんなLaravelのFormRequestを用いたバリデーションで、アップロードファイルのバリデーションも行うことができる。たとえば、以下のようにすることで、ファイルをアップロードを2MB(2048KB)までに限定することができるはずである。

1'form_file' => ['nullable', 'file', 'max:2048'] 2

ところが、2MB以上のファイルをアップロードしてもバリデーションエラーになってくれなかった...
文法的にはまちがってないんだけどな...

Laravelのissueでバグ報告がないか調べると、1件だけ類似の報告があった。が、取り合ってもらえてない。

https://github.com/laravel/framework/issues/37933

バグの可能性も残りつつ、とりあえずコードを読んでみよう!ということで、Validator周りのコードを読むことになった。

Laravel Code Reading!!!

まず、FormRequestに定義したバリデーションルールがどのように読み込まれ、どのように実行されているのかを追うことにする。
バリデーション済みの値を取得するのは、validated()というメソッドである。ここからみていこう。

vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php

1/** 2 * Get the validated data from the request. 3 * 4 * @return array 5 */ 6 public function validated() 7 { 8 return $this->validator->validated(); 9 } 10 11

この
$this->validator
は、

vendor/laravel/framework/src/Illuminate/Contracts/Validation/Validator.php

で定義されているinterface Validator class を想定している。
これの実装は、

vendor/laravel/framework/src/Illuminate/Validation/Validator.php

である。実装されたvalidated()を確認すると...

1 2 /** 3 * Get the attributes and values that were validated. 4 * 5 * @return array 6 * 7 * @throws \Illuminate\Validation\ValidationException 8 */ 9 public function validated() 10 { 11 if ($this->invalid()) { 12 throw new ValidationException($this); 13 } 14 15 $results = []; 16 17 $missingValue = Str::random(10); 18 19 foreach (array_keys($this->getRules()) as $key) { 20 $value = data_get($this->getData(), $key, $missingValue); 21 if ($value !== $missingValue) { 22 Arr::set($results, $key, $value); 23 } 24 } 25 26 return $results; 27 } 28 29

この$this->getRules()の中にはちゃんとFormRequestを継承して作ったRequestファイルに定義されたルールが確認できた。(ddで確認したら本当にruleが入っている連想配列だった)

$valueも同様にvar_dump()で値を確認したところ、少なくとも一時的なファイル名を表す記号列を確認できたのでここまでは多分大丈夫。

いや、ここではvalidate()が呼ばれてない、だと... ? Arr::setもdata_getもデータの形かえてるだけじゃん...

もしや、$this->invalid() お主か?

1 2/** 3 * Returns the data which was invalid. 4 * 5 * @return array 6 */ 7 public function invalid() 8 { 9 if (! $this->messages) { 10 $this->passes(); 11 } 12 13 $invalid = array_intersect_key( 14 $this->data, $this->attributesThatHaveMessages() 15 ); 16 17 $result = []; 18 19 $failed = Arr::only(Arr::dot($invalid), array_keys($this->failed())); 20 21 foreach ($failed as $key => $failure) { 22 Arr::set($result, $key, $failure); 23 } 24 25 return $result; 26 } 27

$this->passes()それっぽいのいるぞ!

1/** 2 * Determine if the data passes the validation rules. 3 * 4 * @return bool 5 */ 6 public function passes() 7 { 8 $this->messages = new MessageBag; 9 10 [$this->distinctValues, $this->failedRules] = [[], []]; 11 12 // We'll spin through each rule, validating the attributes attached to that 13 // rule. Any error messages will be added to the containers with each of 14 // the other error messages, returning true if we don't have messages. 15 foreach ($this->rules as $attribute => $rules) { 16 if ($this->shouldBeExcluded($attribute)) { 17 $this->removeAttribute($attribute); 18 19 continue; 20 } 21 22 foreach ($rules as $rule) { 23 $this->validateAttribute($attribute, $rule); 24 25 if ($this->shouldBeExcluded($attribute)) { 26 $this->removeAttribute($attribute); 27 28 break; 29 } 30 31 if ($this->shouldStopValidating($attribute)) { 32 break; 33 } 34 } 35 } 36 37 // Here we will spin through all of the "after" hooks on this validator and 38 // fire them off. This gives the callbacks a chance to perform all kinds 39 // of other validation that needs to get wrapped up in this operation. 40 foreach ($this->after as $after) { 41 $after(); 42 } 43 44 return $this->messages->isEmpty(); 45 } 46

念のため、$ruleをvar_dumpしてみると、以下のように設定したバリデーションルールが得られた。

1string(4) "file" string(8) "max:2048" string(9) "mimes:pdf" 2

ここら辺はよくわからないので、var_dump()を仕掛けるだけ仕掛けて、デバッグだ。

validateAttribute がvalidationを実行しているっぽいぞ。

1/** 2 * Validate a given attribute against a rule. 3 * 4 * @param string $attribute 5 * @param string $rule 6 * @return void 7 */ 8 protected function validateAttribute($attribute, $rule) 9 { 10 $this->currentRule = $rule; 11 12 [$rule, $parameters] = ValidationRuleParser::parse($rule); 13 14 if ($rule == '') { 15 return; 16 } 17 18 // First we will get the correct keys for the given attribute in case the field is nested in 19 // an array. Then we determine if the given rule accepts other field names as parameters. 20 // If so, we will replace any asterisks found in the parameters with the correct keys. 21 if (($keys = $this->getExplicitKeys($attribute)) && 22 $this->dependsOnOtherFields($rule)) { 23 $parameters = $this->replaceAsterisksInParameters($parameters, $keys); 24 } 25 26 $value = $this->getValue($attribute); 27 28 // If the attribute is a file, we will verify that the file upload was actually successful 29 // and if it wasn't we will add a failure for the attribute. Files may not successfully 30 // upload if they are too large based on PHP's settings so we will bail in this case. 31 if ($value instanceof UploadedFile && ! $value->isValid() && 32 $this->hasRule($attribute, array_merge($this->fileRules, $this->implicitRules)) 33 ) { 34 return $this->addFailure($attribute, 'uploaded', []); 35 } 36 37 // If we have made it this far we will make sure the attribute is validatable and if it is 38 // we will call the validation method with the attribute. If a method returns false the 39 // attribute is invalid and we will add a failure message for this failing attribute. 40 $validatable = $this->isValidatable($rule, $attribute, $value); 41 42 if ($rule instanceof RuleContract) { 43 return $validatable 44 ? $this->validateUsingCustomRule($attribute, $value, $rule) 45 : null; 46 } 47 48 $method = "validate{$rule}"; 49 50 if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) { 51 $this->addFailure($attribute, $rule, $parameters); 52 } 53 } 54

ファイルアップロードに失敗したらここで弾かれるようだ。ここは通っている。

1// If the attribute is a file, we will verify that the file upload was actually successful 2 // and if it wasn't we will add a failure for the attribute. Files may not successfully 3 // upload if they are too large based on PHP's settings so we will bail in this case. 4 if ($value instanceof UploadedFile && ! $value->isValid() && 5 $this->hasRule($attribute, array_merge($this->fileRules, $this->implicitRules)) 6 ) { 7 return $this->addFailure($attribute, 'uploaded', []); 8 } 9
1/** 2 * Returns whether the file was uploaded successfully. 3 * 4 * @return bool True if the file has been uploaded with HTTP and no error occurred 5 */ 6 public function isValid() 7 { 8 $isOk = \UPLOAD_ERR_OK === $this->error; 9 10 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname()); 11 } 12

$methodをvar_dumpしたら呼び出されるvalidationメソッド名が得られた。ここではファイルサイズのバリデーションを疑っているので、validatedMaxを見に行くことにしよう。

1string(4) "file" string(12) "validateFile" 2string(8) "max:2048" string(11) "validateMax" 3string(9) "mimes:pdf" string(13) "validateMimes" 4
1"max:2048" string(3) "Max" bool(false) bool(true) bool(true) bool(true) " 2

なんだと、ここではvalidation していないだと?

他のフォームで文字数制限をmaxで指定している部分は、確かにここでvalidateMaxを呼び出している。

Validator最初で読み込んでいた。traitだから、ValidatorにvalidateMax()があるかのように振る舞えるんだな。

1use Concerns\FormatsMessages, 2 Concerns\ValidatesAttributes; 3

トレイトについて

vendor/laravel/framework/src/Illuminate/Validation/Concerns/ValidatesAttributes.php 1176~

ここで、var_dump($attribute);を行い、validateMax()がファイルアップロード時に呼ばれていないことを確認した。

1/** 2 * Validate the size of an attribute is less than a maximum value. 3 * 4 * @param string $attribute 5 * @param mixed $value 6 * @param array $parameters 7 * @return bool 8 */ 9 public function validateMax($attribute, $value, $parameters) 10 { 11 $this->requireParameterCount(1, $parameters, 'max'); 12 13 if ($value instanceof UploadedFile && ! $value->isValid()) { 14 return false; 15 } 16 return $this->getSize($attribute, $value) <= $parameters[0]; 17 } 18

throw new Exceptionすると、どこで何が呼ばれているのか追えてありがたいw

ここまでの状況をまとめると...
validateAttributeのvalidatableがFalseなので、validateMaxが呼ばれていないみたい。

validatableがなぜFalseなのかを確認する。
validatePresentに渡されるファイルの値がnullになっていた。

presentOrRuleIsImplicit($rule, $attribute, $value)

$valueの取得に失敗してそう。

form-file, form_file の違いで、正しく値を取得できていなかっただけだった。

バグを疑ってごめんなさい、Laravelさん...

まとめ

LaravelのFormRequestでアップロードファイルサイズのバリデーションが動かない... ということで、
コードを読んだ結果、formにつけているname名とこのFormRequestに定義しているname名が一致していないことが原因だった... この程度のミスで一体どれだけ時間を溶かしたんだ...

Hero

まさき。です。PHPエンジニアをやってます。

自分の課題を技術で乗り越えるの好きかもしれないです。

フロントエンドは苦手ですが、少しでもできるようになれたらな、ということでNextJSでこのブログサイトを作りました。