skip to Main Content

I have the mutator as the example in https://backpackforlaravel.com/docs/5.x/crud-fields#image-pro

But I had trouble including the validation rules.

I tried with 'photo' => 'nullable|base64image|base64mimes:jpg|base64max:2048',

And it works when I upload a new image, but if I have an image and try to edit another field it flags the errors:

"The photo field must be an image."

"The photo field must be a file of type: jpg."

Does anyone have any idea why this is happening? or what is the correct way to validate with the pro image field?

2

Answers


  1. namespace AppHttpRequests;
    
    use IlluminateFoundationHttpFormRequest;
    
    class YourFormRequest extends FormRequest
    {
        public function rules()
        {
            return [
                'pro_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust the allowed file types and maximum file size as per your requirements
            ];
        }
    }
    

    namespace AppHttpControllers;

    use AppHttpRequestsYourFormRequest;
    use IlluminateHttpRequest;

    class YourController extends Controller
    {
    public function store(YourFormRequest $request)
    {
    // The request is valid, proceed with storing the data or handling the file
    // The uploaded file is available via $request->file(‘pro_image’)

        // Example: Storing the uploaded file
        $proImage = $request->file('pro_image');
        $proImage->store('your-directory'); // Adjust the storage path as per your requirements
    
        // Additional processing or database storage can be done here
    
        // Redirect or return a response
    }
    

    }

    Login or Signup to reply.
  2. i do this validation with custom validator extend, like this:

    app/Providers/AppServiceProvider.php

    <?php
    
    namespace AppProviders;
    
    use IlluminateSupportServiceProvider;
    use IlluminateSupportFacadesValidator;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         */
        public function register(): void
        {
            //
        }
    
        /**
         * Bootstrap any application services.
         */
        public function boot(): void
        {
            Validator::extend('base64image',function($attribute, $value, $params, $validator) {
                $explode = explode(',', $value);
                $allow = ['png', 'jpg', 'svg'];
                $format = str_replace(
                    [
                        'data:image/',
                        ';',
                        'base64',
                    ],
                    [
                        '', '', '',
                    ],
                    $explode[0]
                );
    
                // check file format
                if (!in_array($format, $allow)) {
                    return false;
                }
    
                // check base64 format
                if (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $explode[1])) {
                    return false;
                }
    
                return true;
            });
        }
    }
    

    app/Http/Requests/XYZRequest.php

    'photos' => 'required|base64image',
    

    Is working always.

    Cheers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search