jeudi 8 mars 2018

returning validator errors to view laravel

I've made a function that validates data, if the data passes the validator it should post the data to the database. If it doesn't pass it should pass an error to the view page.

The error part doesnt work, maybe you have any idea why?

Controller:

public function new(Request $request) : string
{
    $errors = [];
     if ($request->isMethod("post")) {
    $valid = $request->validate([
        "order" => "required|min:1"
    ]);
    if($valid){
        $product = Products::create([
            "title" => $request->input("title"),
            "order" => $request->input("order"),
            "Prijs" => $request->input("prijs"),
            "body" => $request->input("body"),
        ]);
     return redirect("/admin/products/" . $product->id);
    } else {
        $errors = ["fout"];
    }
}
  return View("admin.products.new", [
            "errors" => $errors,
        ]);

}

    @extends("admin.base")

@section("content")
    @foreach ($errors as $error)
    <p class="error"></p>
    @endforeach


    <div class="form_group">
        
        
    </div>
    <div class="form_group">
        
         
    </div>
     <div class="form_group">
        
         
    </div>
    <div class="form_group">
        
         
    </div>

    <div class="form_group">
        
    </div>


@stop

Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tsQ6ir
via IFTTT

Aucun commentaire:

Enregistrer un commentaire