Article From:https://www.cnblogs.com/hualingyun/p/9123198.html

1、It is the most basic method to write form verification logic in Controller, but it is difficult to maintain such as:

 1 namespace App\Http\Controllers\Admin;
 2 
 3 use Illuminate\Http\Request;
 4 use App\Http\Controllers\Controller;
 5 
 6 class MemberController extends Controller
 7 {
 8     // Login module
 9     public function login (Request $request){
10         if($request->isMethod('POST')){
11             $this->validate($request,[
12                 'username' => 'required|max:10',
13                 'password' => 'required|between:6,20',
14                 ],[
15                 'username.required' => 'The username must be filled ',16                 'password.required' => 'The password must be filled ',17                 ]);
18     
19         }
20         return view('admin.login');
21     }
22 }

In this way, the form validation and the business logic are squeezed together, we have too much code in our Controller, and the repeated validation rules are basically copied and pasted.

We can use Form Request to encapsulate form validation code, so as to streamline the code logic in Controller and focus on business. The independent form validation logic can even be reused to other requests.

 

2、First, we open the DOS command and enter the root directory of the project to create the request validation class:

3、MemberRequestCommon validation module code

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class MemberUserRequest extends FormRequest
{
    // Public part
    public $rules=[
        'username' => 'required|max:10|unique:la_member',
        'password' => 'required|between:6,20|confirmed',

    ];

    //Here I have written only some fields, which can define all fields.
    protected $messages=[
        'username.required' => 'The username must be filled ','username.max'=>' username is up to 10 characters','username.unique'=>' username has already existed ','email.required'=>' mailbox must fill ','email.email'=>' mailbox format error 11','password.required'=> 'password must be filled','password.between'=>' password length is 6-20 bit characters'.'password.confirmed'=>' twoThe secondary cipher is inconsistent ','code.required'=>' verifying code is not empty ','code.between'=>' verifying code input error '
     ];


    public function authorize()  //This method can be used to control access rights, such as prohibiting unpaid user access.
    {
        return true; //The default is false, which is changed to true when used.
    }

    public function rules()
    {
        $rules=$this->rules;
        // \Request::getPathInfo()Method to get named routes to distinguish different pages
        if(\Request::getPathInfo()=='/admin/reg_sub'){ //Routing is a specific validation part of /admin/reg_sub
            $rules['email']='required|email';
            $rules['code']='required|between:4,4';
        }
        return $rules;
    }

    public function messages(){  //Returns the custom message, and does not use it as the default prompt.
        return $this->messages;
    }

}

4、Controller code

 3 namespace App\Http\Controllers\Admin;
 4 
 5 use Illuminate\Http\Request;
 6 use App\Http\Controllers\Controller;
 7 use Crypt;
 8 use DB;10 
11 
12 class MemberController extends Controller
13 {
14     // Login module
15     public function login (){
16         return view('admin.login');
17     }
18     // Login submission
19     public function login_sub (\App\Http\Requests\MemberRequest $request){  //After the validation controller is introduced, it will automatically verify without any other operations.
20         $username = $request->input('username');
21         $password = $request->input('password');
22         $data=[
23             'username' => $username,
24             'password' => Crypt::encrypt($password),
25             'login_time' => time(),
26         ];
27 
28        //Query database verification login code
29        
30     }
31 }

5、Landing view module

@extends('admin/public/pub')
@section('content')
<div id="myform">
    <center><h3>New user < /h3> < /center>< form method= "post" action= "{{url ('admin/login_sub')", "id=" "&g"T;
        {{csrf_field()}}
        <fieldset>
            <legend>Users log on to < /legend>< div>< labelfor="Name">Username < /label>< input type= "text" name= "username" value= "{{old ('username')"class="input" id="Name" size="20" maxlength="30"/>
                <br>
            </div>
            <div>
                <label for="password">Input password < /label>< input type= "password" name= "password" value= "{{old" ('password'))]] "class="input" id="password" size="18" maxlength="15"/>
            </div>
            <div>
            </div>
            <div class="enter">
                <input name="login" type="submit" class="buttom" value="Landing "/>< a href= "{{url ('admin/reg')"; > registered < /a>< /div>< /fieldset>< /form>< br>
    @if(count($errors)>0)
        @foreach($errors->all() as $error)
            {{$error}}
        @endforeach
    @endif
</div>
@endsection

Prompt message code in view

1 @if(count($errors)>0)
2         @foreach($errors->all() as $error)
3             {{$error}}
4         @endforeach
5 @endif

It can also be prompted by a single line.

{{$errors->first()}}

 

Here are some of my experiences to make rules () reusable and add only one Request. If you have other methods or ideas, you can talk with me in the comments.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *