欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Laravel Validator

程序员文章站 2022-06-01 20:02:38
Controller use Validator; public function Validators(){ $rules = [ 'title' => 'required|max:255', 'content' => 'required|max:255', 'field' => 'require ......

controller

  use validator;

  public function validators(){

  $rules = [

    'title' => 'required|max:255',

    'content' => 'required|max:255',

    'field' => 'required|max:255',

  ];

  $messages = [

    'title.required' => 'error message',

    'content.required => 'content error message ',

    'field.required' => 'field error message',

  ];

  $validator = validator::make($request -> all(), $rules , $messages);

  if($validator -> fails()){

    return redirect('/path') -> witherrors($validator) -> withinput();

  }

 

blade templement

   @if($errors -> any())

     @foreach($errrors -> all() as $error)

       {{ $error }}

     @endforeach 

   @endif