Laravel 5.2 validation errors

后端 未结 9 709
隐瞒了意图╮
隐瞒了意图╮ 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: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)
        
      @foreach ($errors->all() as $error)
    • {{ $error }}
    • @endforeach
    @endif

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

    Route::group(['middleware' => ['web']], function () {
    
        Route::resource('RouteURL', 'ControllerName');
    });
    

提交回复
热议问题