Reputation: 21
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?
UPDATE
Apparently with backpack's "image Pro" field, I get a base64 image when I select the image from my computer, so it validates fine when this happens. But when another field is edited, what is obtained from the "photo" field is, for example: "actors/photos/739503e96ef392255f6266ce3bf1dded.jpg" that is, the path where the image is saved, so the base64 validation fails.
Upvotes: 0
Views: 569
Reputation: 1
you should tried to handle validation both jpg and base64 encode I think when you select an image it will convert it to base64 encode but when you edit it, it will pick the existing path that have .jpg instead. sorry I don't have a code.
Upvotes: 0
Reputation: 391
i do this validation with custom validator extend, like this:
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
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
Upvotes: 0
Reputation: 101
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
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 App\Http\Controllers;
use App\Http\Requests\YourFormRequest; use Illuminate\Http\Request;
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
}
}
Upvotes: 1