Laravel 5.2 validation errors

后端 未结 9 620
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 06:41

I have some trouble with validation in Laravel 5.2 When i try validate request in controller like this

$this->validate($request, [
                \'title         


        
相关标签:
9条回答
  • 2020-11-30 07:26

    Try using

    return redirect()->back()
                  ->withInput($request->all())
                  ->withErrors($validator->errors()); // will return only the errors
    
    0 讨论(0)
  • 2020-11-30 07:26
    // Replace
    
    Route::group(['middleware' => ['web']], function () {
        // Add your routes here
    });
    
    // with 
    
    Route::group(['middlewareGroups' => ['web']], function () {
        // Add your routes here
    });
    
    0 讨论(0)
  • 2020-11-30 07:29

    I have my working validation code in laravel 5.2 like this

    first of all create a function in model like this

    In model add this line of code at starting

    use Illuminate\Support\Facades\Validator;

    public static function validate($input) {
    
                $rules = array(
                    'title' => 'required',
                    'content.*.rate' => 'required',
                  );
                return Validator::make($input, $rules);
            }
    

    and in controller call this function to validate the input

    use Illuminate\Support\Facades\Redirect;

      $validate = ModelName::validate($inputs);
        if ($validate->passes()) {
              ///some code
         }else{
               return Redirect::to('Route/URL')
                                ->withErrors($validate)
                                ->withInput();
          }
    

    Now here comes the template part

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

    and Above all the things you must write your Route like this

    Route::group(['middleware' => ['web']], function () {
    
        Route::resource('RouteURL', 'ControllerName');
    });
    
    0 讨论(0)
提交回复
热议问题