0

I'm having some troubles with errors handling.

I've made a custom Request:

  public function rules()
{
    return [
        "name" => "required|min:3|max:50|string",
        "price" => "required|numeric",
        "visibility" => "required",
        "description" => "required|string|min:10|max:50",
        "image_url" => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        "genre" => "required"
        
    ];
}

In my controller i have:

public function store(dishValidate $request)
{
    $data = $request->validated();

    $file = $request->file('image');
    $extension = $file->getClientOriginalExtension();
    Storage::disk('public')->put($file->getFilename() . '.' . $extension,  File::get($file));

    $dish = new Dish;
    $dish["name"] = $data["name"];
    $dish["description"] = $data["description"];
    $dish["price"] = $data["price"];
    $dish["visibility"] = $data["visibility"];
    $dish["image_url"] = $file->getFilename() . '.' . $extension;

    $dish->getRestaurant()->associate(Auth::User()->getRestaurant->id);
    $dish->getGenre()->associate($data["genre"]);
    $dish->save();
    
    $item = "Hai creato un nuovo piatto con successo!";
    return route("success", compact("item"));
    
}

And in my blade:

    @if ($errors->any())
            <div class="alert alert-danger">
                    <ul>
                @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
                @endforeach

I can't seem to have access to any errors. Is there also a way to keep input if validation fails?

Uxe
  • 55
  • 7

2 Answers2

1

you can define validation rules like this.

Method:-1

$request->validate([
        'name' => 'required',
        'email' => 'required',
        'img' => 'required | mimes:jpeg,jpg,png',
    ]);

then in blade view file, you will return errors array using $error->all()

@foreach ($errors->all() as $error)
    <p>{{ $error }}</p>
@endforeach

Or You can also define it like this below another method( for get validation error in controller function),

Method:-2

step:1- include validator class

use Illuminate\Support\Facades\Validator;

step 2: define rule in controller function

$validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email',
        'password' => 'required',
        'c_password' => 'required|same:password',
    ]);
print_r($$validator->messages());

If you want to make validation in API I recommend use method-2.

Harsh Patel
  • 1,032
  • 6
  • 21
  • I would like to keep the code clean and use a custom Request. There's not way to access error using Request? – Uxe Mar 03 '21 at 12:51
  • https://stackoverflow.com/questions/28793716/how-add-custom-validation-rules-when-using-form-request-validation-in-laravel-5 check out this question, I hope this one helps to you – Harsh Patel Mar 03 '21 at 12:55
  • `dishValidate $request` this contain validation. We don't define it again in the controller with `Validator::make($` – Abdulla Nilam Mar 03 '21 at 13:00
0

I solved. Apparently i'm stupid and i spent 1.5 hour in the wrong blade file.

Everything works fine with a custom Request.

Uxe
  • 55
  • 7