Laravel 5.1 framework form verification operation example details

Time:2021-10-22

This article describes the form verification operation of laravel 5.1 framework. Share with you for your reference, as follows:

When we submit a form, we usually verify the submitted data. Laravel uses a trail: validatesrequest in the controller class. It is convenient for us to use the verifier in the controller.

Let’s take a look at an example of a validation form.

1 Preparation

1.1 create route


Route::resource('/post', 'PostController');

1.2 create controller


php artisan make:controller PostController

1.3 creating views

Create the / post / create.blade.php file in / views as follows:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div>
  <div>
    <div>
      <div>
        <div>
          Create article
        </div>
        <div>
          <form action="{{ url("/post") }}" method="POST">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div>
              < label > Title < / label >
              <div>
                <input type="text" name="title">
              </div>
            </div>
            <div>
              < label > content < / label >
              <div>
                <textarea rows="10" name="content"></textarea>
              </div>
            </div>
            <div>
              <div>
                <button type="submit">Submit</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

1.4 return to the create view in the postcontroller


public function create()
{
    return view('post.create');
}

2 start verification

2.1 validate

We verify the data submitted by the form in the store method. The syntax is as follows:

Validate() parameter:

  1. Request: just pass in the request.
  2. Rule: a rule array. Write our verification logic in it.
public function store(Request $request)
{
    $this->validate($request, [
      'title' => 'required|min:3',
      'content' => 'required|min:10',
    ]);
    Echo 'verification passed';
}

↑ in the above example, if the verification passes, it will display “verification passes”. If the verification fails, laravel will automatically jump to the form submission page and flash the error information into the session. We can modify the create.balde.php file to add the displayed error code

2.2 display error message

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" >
</head>
<body>
<div>
  <div>
    <div>
      <div>
        <div>
          Create article
        </div>
        <div>
          @if (count($errors) > 0)
            <div>
              <ul>
                @foreach ($errors->all() as $error)
                  <li>{{ $error }}</li>
                @endforeach
              </ul>
            </div>
          @endif
          <form action="{{ url("/post") }}" method="POST">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div>
              < label > Title < / label >
              <div>
                <input type="text" name="title">
              </div>
            </div>
            <div>
              < label > content < / label >
              <div>
                <textarea rows="10" name="content"></textarea>
              </div>
            </div>
            <div>
              <div>
                <button type="submit">Submit</button>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

3 Create validator manually

public function store(Request $request)
  {
//    $this->validate($request, [
//      'title' => 'required|min:3',
//      'content' => 'required|min:10',
//    ]);
    $validator = Validator::make($request->all(), [
      'title' => 'required|min:3',
      'content' => 'required|min:10',
    ]);
    if ($validator->fails()) {
      return redirect('post/create')
        ->withErrors($validator)
        ->withInput();
    }
    Echo 'verification passed';
  }

For more information about laravel, readers who are interested can see the special topics on this site: introduction and advanced tutorial of laravel framework, summary of PHP excellent development framework, introduction to PHP object-oriented programming, introduction to PHP + MySQL database operation, and summary of common database operation skills for PHP

I hope this article will be helpful to your PHP Programming Based on laravel framework.