2017-02-02 10 views
2

バリデータが失敗した場合は私のPOSTフォームをリダイレクトしようとしましたが、GETルートではないため動作しません。 私は製品を選択するための最初のページを持っていて、2番目のページに投稿して1つのモデルを製品に追加しています。 バリデータが失敗した場合、2ページ目にリダイレクトしたいです。Laravel - ルートへのリダイレクトPOST

検証

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

ルート:

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 

コントローラー:

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'));   
     } 


    } 

モデルのページを作成します

 {!! 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() !!} 

:ページを選択し

    {!! 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

にポストする要求を行うことができない私たちは、ページの規則を教えてくださいそこにはルートページがありますか?コントローラで検証を実行する必要があります。 – DudeOfLayers

+0

経路ページの確認を確認していますか? –

+0

あなたは私達にもっと情報を与えるべきです。 –

答えて

0

あなたが要求を取得確認する必要があります。..その場合

+0

どうしたらいいですか? – emeliku

関連する問題