Search code examples
phplaravelvalidationfaviconlaravel-5.7

Validate favicon on controller from form


i want to validate my file, only .ico files. Laravel dont include x-icon mime I think, how can i validate it?

  $logo = $request->file('logo');
  $favicon = $request->file('favicon');

  $request->validate([
    'logo'=>'image|mimes:png',
    'favicon'=>'',
     ]);

Solution

  • Make a custom validation rule as explained here.

    In short:

    First do:

    php artisan make:rule CheckIfFavicon
    

    Then:

    Create the validation code in the created Rules-file.

    Try something like:

      public function passes($attribute, $value)
            {
                return $value->getClientOriginalExtension() == 'ico';
            }
    

    Then ad it to the validation. Note, that if you make a custom validation class you will have to change the syntax in the $request->validate([...]) from pipe-ing to array.

    $request->validate([
        'favicon' => [new CheckIfFavicon], 
    ]);