2017-02-02 2 views
2

Ich habe versucht, meine POSTform umleiten, wenn Validierung fehlschlägt, aber es funktioniert nicht, weil es keine GET-Route ist. Ich habe eine erste Seite für die Auswahl eines Produkts, Post auf die zweite Seite für ein Modell zum Produkt hinzufügen. Ich möchte auf die zweite Seite umleiten, wenn der Validator fehlschlägt.Laravel - Umleiten zu einer Route POST

Validation:

if ($validator->fails()) { 
     return redirect('admin/modele/create') 
      ->withErrors($validator) 
      ->withInput(); 
} 

Routen:

Route::get('modele/select/product', '[email protected]')->name('modele.selectProduct'); //get page for select product 
Route::post('modele/selected/product', '[email protected]')->name('modele.selectedProduct'); //the product is selected, send it to form page 
Route::post('modele/create/product/{produit}', '[email protected]')->name('modele.storea'); //is a store custom 

Controller:

public function selectProduct() 
    { 
     // 
      $lesProduits = Produit::pluck('nom', 'id'); 
      return view('admin/modele/select', compact('lesProduits')); 

    } 

    public function selectedProduct(Request $request) 
    { 
     // 

      $unProduit = $request->get('produit'); 
      $id = ($request->get('produit')); 

      $unProduit = Produit::where('id', $id)->pluck('nom', 'id'); 
      $uneCategorie = Produit::find($id)->categorie; 
      $produit = Produit::find($id); 
      //dd($uneCategorie) ; 
      //$categorie = $unProduit->categorie->id; 

      return view('admin/modele/create', compact('unProduit', 'uneCategorie', 'produit')); 

    } 
public function storea(Request $request, $produit_id) 
    { 
     // 
     $validator = Validator::make($request->all(), [ 
      'ref' => 'required|max:255', 
      'prixHT' => 'required|max:9|numeric', 
      'prixHTpromo' => 'max:9|numeric', 
      'quantite' => 'required|numeric|max:255', 
      't1' => 'max:255', 
      't2' => 'max:255', 
      't3' => 'max:255', 
      't4' => 'max:255', 
      't5' => 'max:255', 
      't6' => 'max:255', 
      't7' => 'max:255', 
      't8' => 'max:255', 
      't9' => 'max:255', 
      't10' => 'max:255', 
      'image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048', 

     ]); 

     if ($validator->fails()) { 
      return redirect('admin/modele/create') 
         ->withErrors($validator) 
         ->withInput(); 
     } 
     else 
     {   
      $unModele = New Modele(); 
      $unModele->produit_id = $produit_id; 
      $unModele->ref = $request->get('ref'); 
      $unModele->prixHT = $request->get('prixHT'); 
      if ($request->get('prixHTpromo') != null) 
      { 
       $unModele->prixHPromoHT = $request->get('prixHTpromo'); 
      }  
      $unModele->quantiteDebut = $request->get('quantite'); 
      for ($i = 1; $i < 11; $i++) 
      { 
       $name = 't'.$i; 
       $nameDB = 'info'.$i; 
       if ($request->get($name) != null) 
       { 
        $unModele->$nameDB = $request->get($name); 
       } 
      } 
      // photo 0,1    
      if($request->file('image') != null) // && $uneActualite->url != null) 
      { 
       //image code here 
      } 
      else 
      { 
       $unModele->image=null; 
      } 
      ///////////////////////// 
      $unModele->save(); 
      $request->session()->flash("success","Modèle ajouté."); 
      return redirect(route('modele.index'));   
     } 


    } 

Wählen Sie Seite:

 {!! Form::open(array('route' => 'modele.selectedProduct')) !!} 


          <div class="form-group"> 
       {!! Form::label('produit', 'Produit') !!} 
       {!! Form::select('produit',$lesProduits, null, array('class' => 'form-control')) !!} 
          </div> 
       @if ($errors->has('produit')) 
          <div class="alert alert-danger" role="alert"> 
           <ul> 
            @foreach ($errors->get('produit') as $message) 
             <li>{{$message}}</li> 
            @endforeach 
           </ul> 
          </div> 
          @endif 




          <button type="submit" class="btn btn-primary col-lg-12"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button> 
          {!! Form::close() !!} 

erstellen Modellseite:

    {!! Form::open(array('route' => ['modele.storea', $produit->id],'enctype' => 'multipart/form-data')) !!} 


        <div class="form-group"> 
     {!! Form::label('produit', 'Produit') !!} 
     {!! Form::select('produit', $unProduit, null, array('class' => 'form-control')) !!} 
        </div> 
     @if ($errors->has('produit')) 
        <div class="alert alert-danger" role="alert"> 
         <ul> 
          @foreach ($errors->get('produit') as $message) 
           <li>{{$message}}</li> 
          @endforeach 
         </ul> 
        </div> 
        @endif 

        <div class="form-group"> 
        {!! Form::label('ref', 'Réference') !!} 
        {!! Form::text('ref', null,array('maxlength' => 255, 'class'=>'form-control')) !!}       
        </div>       
        @if ($errors->has('ref')) 
        <div class="alert alert-danger" role="alert"> 
         <ul> 
        @foreach ($errors->get('ref') as $message) 
         <li>{{$message}}</li> 
        @endforeach 
        </ul> 
        </div> 
        @endif 
@for ($i = 1; $i < 11; $i++) 
           <?php $item_name = 't' . $i; ?> 

           @if ($uneCategorie->$item_name != null) 
            <div class="col-lg-3"> 
             <div class="form-group"> 
             {!! Form::label($item_name, $uneCategorie->$item_name) !!} 
             {!! Form::text($item_name, null,array('maxlength' => 255, 'class'=>'form-control')) !!}       
             </div>       
             @if ($errors->has('{{$item_name }}')) 
             <div class="alert alert-danger" role="alert"> 
              <ul> 
             @foreach ($errors->get('{{$item_name }}') as $message) 
              <li>{{$message}}</li> 
             @endforeach 
             </ul> 
             </div> 
             @endif 
            </div> 
           @endif            
          @endfor 
+0

Bitte teilen Sie uns die Seite Konventionen kennen Sie haben dort, das ist die Routen-Seite? Sie sollten Validierungen im Controller durchführen. – DudeOfLayers

+0

Überprüfen Sie die Validierung auf der Routen-Seite? –

+0

Sie sollten uns mehr Informationen geben. –

Antwort

0

Sie Anfrage machen bekommen .. kann nicht Antrag stellen in diesem Fall zu schreiben

+0

Wie mache ich das dann? – emeliku

Verwandte Themen