Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

insight's avatar

"Object of class couldn't be converted to string". How to solve ?

Dear Friends, I am looking a file upload pdf validation for limiting the number of pages in PDF uploaded . I tried many ways . But now get Exception as


    "message": "Object of class App\Rules\MaxPages could not be converted to string",
    "exception": "Error",
    "file": "C:\xampp\htdocs\ksit_mission\vendor\laravel\framework\src\Illuminate\Validation\ValidationRuleParser.php",
    "line": 134,

my AppServiceProvider class is

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use Smalot\PdfParser\Parser;
use App\Rules\MaxPages;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // $this->app['validator']->extend('max_pages', function ($attribute, $value, $parameters, $validator) {
        //     $maxPages = $parameters[0];
        //     $rule = new MaxPages($maxPages);
        //     return $rule->passes($attribute, $value);
        // });
        Validator::extend('max_pages', function ($attribute, $value, $parameters, $validator) {
            if (!is_array($value)) {
                return false;
            }

            $maxPages = $parameters[0];

            foreach ($value as $file) {
                if (!$file->isValid()) {
                    return false;
                }

                $pdf = (new Parser())->parseFile($file->path());
                $numPages = count($pdf->getPages());
                if ($numPages > $maxPages) {
                    return false;
                }
            }

            return true;
        });

    }
}

  1. MaxPages.php in app/Rules
<?php

namespace App\Rules;

use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Validator;
use Smalot\PdfParser\Parser;

class MaxPages extends Rule
{
    protected $maxPages;
    

    public function __construct($maxPages)
    {
        $this->maxPages = $maxPages;
    }


   
    public function passes($attribute, $value)
    {
        if (!is_array($value)) {
            return false;
            
        }

        foreach ($value as $file) {
            if (!$file->isValid()) {
                return false;
                
            }

            $pdf = (new Parser())->parseFile($file->path());
            $numPages = count($pdf->getPages());

            if ($numPages > $this->maxPages) {
                return false;
            }
        }

        return true;
    }

    public function message()
    {
        return "The :attribute must have a maximum of {$this->maxPages} pages.";
    }

    
}

call it in 6 places in controller as

'quali_upload.*' => ['required', 'file', 'mimes:pdf', 'max:2048', new MaxPages(3)],  // 2 times called
'upload_additional.*' => ['nullable','file','mimes:pdf','max:2048',new MaxPages(2)], // 2 times called
'uploadworkexperience.*' => ['nullable','mimes:pdf','max:2048',new MaxPages(1)],  // 2 times clalled 

with included in controller

use App\Rules\MaxPages;

Please help

Thanks

Anes P A

0 likes
6 replies
vincent15000's avatar

Why are you declaring an validator extension in the AppServiceProvider.php file ?

According to me it's not necessary.

insight's avatar

@vincent15000 as per your suggestion I remove the relevant code from AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
//use Illuminate\Support\Facades\Validator;
//use Smalot\PdfParser\Parser;
//use App\Rules\MaxPages;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // $this->app['validator']->extend('max_pages', function ($attribute, $value, $parameters, $validator) {
        //     $maxPages = $parameters[0];
        //     $rule = new MaxPages($maxPages);
        //     return $rule->passes($attribute, $value);
        // });
        /*Validator::extend('max_pages', function ($attribute, $value, $parameters, $validator) {
            if (!is_array($value)) {
                return false;
            }

            $maxPages = $parameters[0];

            foreach ($value as $file) {
                if (!$file->isValid()) {
                    return false;
                }

                $pdf = (new Parser())->parseFile($file->path());
                $numPages = count($pdf->getPages());
                if ($numPages > $maxPages) {
                    return false;
                }
            }

            return true;
        });*/

    }
}


I got now BadMethodCallException as shown

"message": "Method Illuminate\Validation\Validator::validateMaxPages does not exist.",
    "exception": "BadMethodCallException",
    "file": "C:\xampp\htdocs\ksit_mission\vendor\laravel\framework\src\Illuminate\Validation\Validator.php",
    "line": 1575,

Please adive

Snapey's avatar

as you have extended the rules, use it as a string

'quali_upload.*' => ['required', 'file', 'mimes:pdf', 'max:2048', 'max_pages:3'], 
2 likes
insight's avatar

@Snapey Your solution works , but it give validation error message for pdf with proper number of pages too .... in essence show validation error for all pdf's... What May be worong ? You can see the inteface like this in web site https://imgur.com/a/WL3EUyL

Please help

1 like
vincent15000's avatar

@insight If you have removed the extended validation, you can't use @snapey's solution, but only the other one with new MaxPages(3).

Snapey's avatar

@insight you are uploading an array, your message applies to all uses of the array, you need to be specific about which array member has the error

Please or to participate in this conversation.