A list,
-
Form verification is to prevent visitors from skipping the client verification caused by the system security problems, but once the illegal users bypass the client verification and the server side is not verified, this is very unsafe, so the project must be server-side form verification.
-
Laravel provides a number of different validation methods to validate incoming data from an application.
-
Common Authentication rules
The rules of instructions required
It cannot be empty. max:value
The field value must be less than or equal to value
For strings,value
Is the number of characters.min:value
The field value must be greater than or equal to value
For strings,value
Is the number of characters.email
Verify that the mailbox is valid. url
The validation field must be valid URL
Format.confirmed
Verify that two fields are the same if the validated field is password
, you must enter a matchingpassward_confirmation
Field.integer
The validation field must be an integer. ip
The validation field must be IP
Address.numeric
Validation fields must be numeric. size:value
value
Validation fields must have and given valuesvalue
Size to match; For strings,value
Is the corresponding number of characters; In terms of numbers,value
Is the given integer value; For files,value
Is the number of bytes of the corresponding file.string
Validation fields must be strings. unique
Table name, field, whatever you want to exclude ID
.between:min,max
Verifies that the field value is between the specified size min
和max
Between string values or file sizes andsize
The rules are the same.More Validation Rules
Laravel all available validation rules and their functionality.
Second,$this->validate()
(case:Form
Form add user)
-
Web.php: User actions are placed in the UserController file
Route::get('adduser', 'UserController@index') -> name('user.adduser'); Route::post('adduser', 'UserController@save') -> name('user.adduser');Copy the code
-
User /index.blade. PHP: Displays the page for adding a user
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, <meta http-equiv=" x-UA-compatible "content="ie=edge"> <title> I'll just hint, In the template output error message -}} @ the if ($errors - > any ()) < ul > @ foreach ($errors - > all () as $error) < li > {{$error}} < / li > @ endforeach < / ul > @endif {{-- submit to the specified route, Form action="{{route('user.adduser')}}" method="post"> @csrf {{-- use old('account'). When the page is reset, this field will continue to be displayed with the old value of the last submission, and will not clear --}} <div> user account: < input type = "text" name = "account" value = "{{old (" account")}} "> < / div > < div > user password: <input type="password" name=" PWD "></div> <div> <input type="email" name="email" value="{{ old('email') }}"></div> <input style="margin-top: 5px;" Type = "submit" value = "add user" > < / form > < / body > < / HTML >Copy the code
-
Usercontroller.php: UserController page
<? php namespace App\Http\Controllers; use Illuminate\Http\Request; Class UserController extends Controller {public function index() {return view('user.index'); Public function save(Request $Request) {$Request ->all()); Dump ($request->except(['_token'])); $request->except(['_token']); // Laravel 5.6 returns a $input, $input = $this->validate($request, $token); [/ / field name = > rule name with a | (rules) 'account' = > 'required | between: 2, 6', / / confirm password confirmed attributes must be written in the book of the original password 'PWD' = > 'required | confirmed', 'pwd_confirmation' = > 'required', 'email' = > 'required | email'], [/ / field names. Rule name = > false statements' account. The required '= >' user name cannot be empty. 'Account. Between' => 'Username must be 2-6 characters ', 'pwd.required' =>' Password cannot be empty ', 'pwd.confirmed' => 'Two different passwords ', 'email. The required' = > 'email can't for empty', 'email. Email' = > 'E-mail format is not correct,]); Dump ($input); }}Copy the code
-
The Demo:
Three,Validator::make()
(Independent verification
)
-
Again, the example code above, now needs to be verified by independent validation.
-
Usercontroller.php: UserController page
<? php namespace App\Http\Controllers; use Illuminate\Http\Request; Use Illuminate\Support\Facades\Validator; // Can be directly abbreviated as the following way // why so short? The /config/app.php file is configured to use Validator. Class UserController extends Controller {public function index() {return view('user.index'); Public function save(Request $Request) {// dump($Request ->all()); Dump ($request->except(['_token'])); $validate = Validator::make($request->all(), [/ / field name = > rule name with a | (rules) 'account' = > 'required | between: 2, 6', / / confirm password confirmed attributes must be written in the book of the original password 'PWD' = > 'required | confirmed', 'pwd_confirmation' = > 'required', 'email' = > 'required | email'], [/ / field names. Rule name = > false statements' account. The required '= >' user name cannot be empty. 'Account. Between' => 'User name must be 2-6 characters ', 'pwd.required' =>' Password cannot be empty ', 'pwd_confirmation. Required '=>' Password cannot be empty ', 'pwd.confirmed' => 'Two different passwords ', 'email.required' => 'email cannot be empty ', 'email.email' => 'email format is incorrect ',]); // dump(get_class_methods($validate)); // dump($validate); If ($validate->fails()) {return redirect()->back()->withErrors($validate); $request->all(); $request->all(); }}Copy the code
Four,Requests
(The validator
)
-
Again, the example code above, which was previously validated within the current method, now needs to be validated by the validator
-
Creating a validator (custom Request class)
$ php artisan make:request UserRequest Copy the code
There will be a Requests folder in the Http folder that will hold all the validators, and it will now contain the newly created custom Request class UserRequest
-
Userrequest. PHP: Writes the validation rule to the UserRequest class
<? php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UserRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ authorize() {authorize() {authorize() {authorize() {authorize(); return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ // Field name = > rule name with a | (rules) 'account' = > 'required | between: 2, 6', / / confirm password confirmed attributes must be written in the book of the original password 'PWD' = > 'required | confirmed', 'pwd_confirmation' = > 'required', 'email' => 'required|email' ]; Public function messages() {return [// field name. Rule name => error statement 'account.required' => 'Username cannot be empty ', 'Account. Between' => 'User name must be 2-6 characters ', 'pwd.required' =>' Password cannot be empty ', 'pwd_confirmation. Required '=>' Password cannot be empty ', 'pwd.confirmed' => 'Two different passwords ', 'email.required' => 'email cannot be empty ', 'email.email' => 'email format is not correct ',]; }}Copy the code
-
Usercontroller.php: the UserController page uses the UserRequest class
<? php namespace App\Http\Controllers; use Illuminate\Http\Request; // Import custom Request class use App\Http\Requests\UserRequest; Class UserController extends Controller {public function index() {return view('user.index'); Public function save(UserRequest $request) {$request->all(); Dump ($request->except(['_token'])); }}}}}}}}}Copy the code
Five, will bring their ownEnglish mistakes
Switch to aMistakes in Chinese
- All of the above are custom validation errors. If you do not customize the error, you can switch the default English error of the system configuration to Chinese, which will be more convenient and simple. You can refer to the PHP-Laravel form to switch the verification error to Chinese