najmulcse
4176
13
Laravel

Error messages are not shown after validation

Posted 2 years ago by najmulcse

I want to show error messages after validating of a form . But its not showing the error message when it redirected into previous page . Important point is noted that form validation is working but error message is not shown .

I'm printed for errors checking by {{print_r($errors)}} in my form and its show the following message :Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( ) ) 1

It is assure that the validation is worked properly . When i submitted my form without any values it's not going to other page , it redirected to current page .

#Here i posted my form in a controller method

public function storeLecture(Request $request ){

      $rules=[
          'lecture_title'  => 'required ',
          'body'           => 'required',
          'file'           => 'required'
           ];
     $gid=$request->gid;
     $v= Validator::make($request->all(),$rules);
     //$this->validate($request,$rules);
   
     if($v->fails())
            {
             return dd($v);
             //return redirect()->back()->withErrors($v)
                          
                          ->withInput();
            }
      else {
             return  "Ok";
            }
     
     $file= $request->file('file');
     $user_id = Auth::user()->id;
     $post=Post::create([
      'group_id'  => $gid ,
      'user_id'   => $user_id , 
      'title'     => $request->lecture_title ,
      'body'      => $request->body,
      'type'      => 'L'
                      ]);

     if(!empty($file))
     {
        $content=time().$file->getClientOriginalName();
        $post_id=$post->id;
        $file_store=Content::create(['post_id' => $post_id ,'content' =>$content ]);
        $file->move('postfiles',$file_store->id);
     }


      return redirect()->route('allLectures',$gid);  
      

    }

#my form blade file

<form action="{{route('storeLecture')}}" method="POST" enctype="multipart/form-data">
                                      {{ csrf_field() }}                                                             
                                @foreach($errors as $error)
                                      <li>{{$error}}</li>
                                @endforeach 
                                            <input type="hidden" name="gid" value="{{$group->id}}" >
                                      <div class="form-group ">     
                                           <label class="control-label">Lecture Title</label>
                                           <input type="text" name="lecture_title" class="form-control" placeholder="Lecture Title" value="{{old('lecture_title')}}">  
                                     </div>
                                     <div class="form-group">
                                            <label class="control-label">Body</label>
                                            <textarea class="form-control" name="body" rows="5" placeholder="Write here...">{{ old('body') }}</textarea>
                                      </div>
                                     <div class="form-group">
                                           <label class="control-label">File</label>
                                             <input type="file" name="file" class="form-control" accept=".doc,.ppt,.pdf,.jpeg,.png,.jpg," value="{{ old('file') }}">     
                                     </div>
                                     <div class="form-group">

                                          <button type="submit" class="btn btn-sm btn-success pull-right">Upload lecture</button>   
                                    </div>
                           </form> 


#routes are

Route::get('group/{gid}/allLectures',['as' => 'allLectures' , 'uses' => '[email protected]']);
    Route::get('group/{gid}/createLecture',['as' => 'createLecture','uses' => '[email protected]']);
    Route::post('group/Lecture/store',['as' => 'storeLecture','uses' => '[email protected]']);


When i check the return value for showing all validation by dd($v) its catch the validation but when it redirected to return redirect()->back()->withErrors($v) ->withInput();

no error messages are showing in my form .

Please help me to solve this problem .

Please sign in or create an account to participate in this conversation.