Làm cách nào để gửi thông báo lỗi từ chế độ xem Trình điều khiển trong Laravel?

Cách gửi thông báo Lỗi và Thành công tùy chỉnh từ Bộ điều khiển đến Chế độ xem trong Laravel 7


Laravel 7 giúp dễ dàng hơn khi chúng ta cần gửi bất kỳ loại thông báo lỗi hoặc thành công nào hoặc bất kỳ loại thông báo tùy chỉnh nào từ bộ điều khiển của chúng ta đến tệp phiến [ view ]. Vì vậy, trong bài viết này chúng ta sẽ tìm hiểu cách gửi thông báo tùy chỉnh từ bộ điều khiển để xem tệp trong Laravel


Để hiểu điều này chúng ta sẽ lấy một ví dụ để dễ hiểu và chúng ta sẽ sử dụng hàm " ->with[] " do Laravel cung cấp để gửi tin nhắn từ bộ điều khiển để xem tệp. Chúng tôi sẽ sử dụng hộp cảnh báo có thể loại bỏ bootstrap để hiển thị thông báo tùy chỉnh của chúng tôi


Sử dụng cơ bản

bộ điều khiển


return redirect[url['/form']]->with['successmessage','data saved successfully'];


xem tài liệu


@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif


Gửi mẫu xác nhận Lỗi

Chúng tôi cũng có thể gửi lỗi xác thực từ bộ điều khiển đến tệp phiến. Nếu đầu vào biểu mẫu của bạn không khớp với mã xác thực thì nó sẽ tự động chuyển hướng đến tệp phiến của bạn và thực thi mã của bạn để hiển thị lỗi xác thực

Hiển thị thông báo cảnh báo lỗi trong ứng dụng laravel

Bạn chỉ cần thêm phần trả về có thông báo hoặc lỗi và hiển thị trong tệp phiến giống như bên dưới

Cập nhật mã trong Trình điều khiển của bạn
Trước tiên, hãy cập nhật phương thức của bạn với thông báo trả về mà bạn muốn cho người dùng biết họ đặt gì.

return back[]->with['error', 'The error message here!'];

Trong tệp phiến của bạn
Bây giờ hãy thêm mã dưới đây vào tệp phiến của bạn nơi bạn muốn hiển thị thông báo lỗi.

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif

đọc ở đây. Chế độ xem trả về của laravel với thông báo thành công

Laravel cung cấp một số cách tiếp cận khác nhau để xác thực dữ liệu đến của ứng dụng của bạn. Cách phổ biến nhất là sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 có sẵn trên tất cả các yêu cầu HTTP đến. Tuy nhiên, chúng tôi cũng sẽ thảo luận về các cách tiếp cận khác để xác nhận

Laravel bao gồm nhiều quy tắc xác thực thuận tiện mà bạn có thể áp dụng cho dữ liệu, thậm chí cung cấp khả năng xác thực nếu các giá trị là duy nhất trong một bảng cơ sở dữ liệu nhất định. Chúng tôi sẽ trình bày chi tiết từng quy tắc xác thực này để bạn quen thuộc với tất cả các tính năng xác thực của Laravel

Bắt đầu nhanh xác thực

Để tìm hiểu về các tính năng xác thực mạnh mẽ của Laravel, hãy xem một ví dụ hoàn chỉnh về xác thực biểu mẫu và hiển thị lại thông báo lỗi cho người dùng. Bằng cách đọc phần tổng quan cấp cao này, bạn sẽ có thể hiểu rõ hơn về cách xác thực dữ liệu yêu cầu gửi đến bằng cách sử dụng Laravel

Xác định các tuyến đường

Trước tiên, giả sử chúng ta có các tuyến sau được xác định trong tệp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

22 của mình

use App\Http\Controllers\PostController;

Route::get['/post/create', [PostController::class, 'create']];

Route::post['/post', [PostController::class, 'store']];

Tuyến đường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

23 sẽ hiển thị một biểu mẫu để người dùng tạo một bài đăng blog mới, trong khi tuyến đường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

24 sẽ lưu trữ bài đăng blog mới trong cơ sở dữ liệu

Tạo bộ điều khiển

Tiếp theo, chúng ta hãy xem một bộ điều khiển đơn giản xử lý các yêu cầu đến các tuyến này. Bây giờ chúng ta sẽ để trống phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

25

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

Viết logic xác thực

Bây giờ chúng ta đã sẵn sàng để điền logic vào phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

25 của mình để xác thực bài đăng blog mới. Để làm điều này, chúng ta sẽ sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 được cung cấp bởi đối tượng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

28. If the validation rules pass, your code will keep executing normally; however, if validation fails, an

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

29 exception will be thrown and the proper error response will automatically be sent back to the user

If validation fails during a traditional HTTP request, a redirect response to the previous URL will be generated. If the incoming request is an XHR request, a JSON response containing the validation error messages will be returned

To get a better understanding of the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 method, let's jump back into the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

25 method

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
7

As you can see, the validation rules are passed into the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 method. Don't worry - all available validation rules are documented. Again, if the validation fails, the proper response will automatically be generated. If the validation passes, our controller will continue executing normally

Alternatively, validation rules may be specified as arrays of rules instead of a single

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33 delimited string

return back[]->with['error', 'The error message here!'];
0

In addition, you may use the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

34 method to validate a request and store any error messages within a named error bag

return back[]->with['error', 'The error message here!'];
2

Stopping On First Validation Failure

Sometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

35 rule to the attribute

return back[]->with['error', 'The error message here!'];
4

In this example, if the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

36 rule on the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

37 attribute fails, the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

38 rule will not be checked. Rules will be validated in the order they are assigned

A Note On Nested Attributes

If the incoming HTTP request contains "nested" field data, you may specify these fields in your validation rules using "dot" syntax

return back[]->with['error', 'The error message here!'];
8

On the other hand, if your field name contains a literal period, you can explicitly prevent this from being interpreted as "dot" syntax by escaping the period with a backslash

return back[]->with['error', 'The error message here!'];
9

Displaying The Validation Errors

So, what if the incoming request fields do not pass the given validation rules? As mentioned previously, Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors and request input will automatically be flashed to the session

Một biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 được chia sẻ với tất cả các chế độ xem ứng dụng của bạn bởi phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

40, được cung cấp bởi nhóm phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

41. When this middleware is applied an

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 variable will always be available in your views, allowing you to conveniently assume the

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 variable is always defined and can be safely used. The

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 variable will be an instance of

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

45. Để biết thêm thông tin về cách làm việc với đối tượng này, hãy xem tài liệu của nó

Vì vậy, trong ví dụ của chúng tôi, người dùng sẽ được chuyển hướng đến phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

46 của bộ điều khiển của chúng tôi khi xác thực không thành công, cho phép chúng tôi hiển thị thông báo lỗi trong chế độ xem

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
8

Customizing The Error Messages

Laravel's built-in validation rules each have an error message that is located in your application's

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

47 file. Trong tệp này, bạn sẽ tìm thấy mục dịch cho từng quy tắc xác thực. Bạn có thể tự do thay đổi hoặc sửa đổi các thông báo này dựa trên nhu cầu của ứng dụng của bạn

Ngoài ra, bạn có thể sao chép tệp này sang thư mục ngôn ngữ dịch thuật khác để dịch các thông báo cho ngôn ngữ của ứng dụng của bạn. Để tìm hiểu thêm về bản địa hóa Laravel, hãy xem tài liệu bản địa hóa đầy đủ

Yêu cầu & Xác thực XHR

Trong ví dụ này, chúng tôi đã sử dụng một biểu mẫu truyền thống để gửi dữ liệu đến ứng dụng. Tuy nhiên, nhiều ứng dụng nhận được yêu cầu XHR từ giao diện người dùng được hỗ trợ bởi JavaScript. Khi sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 trong một yêu cầu XHR, Laravel sẽ không tạo phản hồi chuyển hướng. Thay vào đó, Laravel tạo một phản hồi JSON chứa tất cả các lỗi xác thực. Phản hồi JSON này sẽ được gửi cùng với mã trạng thái HTTP 422

Chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

49

Bạn có thể sử dụng chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

49 Blade để nhanh chóng xác định xem có tồn tại thông báo lỗi xác thực cho một thuộc tính nhất định hay không. Trong một chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

49, bạn có thể lặp lại biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

52 để hiển thị thông báo lỗi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

5

Nếu bạn đang sử dụng các túi lỗi được đặt tên, bạn có thể chuyển tên của túi lỗi làm đối số thứ hai cho chỉ thị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

49

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

0

Hình thức tái tạo

Khi Laravel tạo phản hồi chuyển hướng do lỗi xác thực, khung sẽ tự động flash tất cả đầu vào của yêu cầu vào phiên. Điều này được thực hiện để bạn có thể truy cập thông tin đầu vào một cách thuận tiện trong yêu cầu tiếp theo và điền lại biểu mẫu mà người dùng đã cố gắng gửi

Để truy xuất đầu vào được flash từ yêu cầu trước đó, hãy gọi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

54 trên phiên bản của

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

28. Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

54 sẽ lấy dữ liệu đầu vào được flash trước đó từ phiên

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

1

Laravel cũng cung cấp một trình trợ giúp toàn cầu

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

54. Nếu bạn đang hiển thị đầu vào cũ trong mẫu Blade, sẽ thuận tiện hơn khi sử dụng trình trợ giúp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

54 để điền lại biểu mẫu. Nếu không có đầu vào cũ nào tồn tại cho trường đã cho, thì

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 sẽ được trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

0

Lưu ý về các trường tùy chọn

Theo mặc định, Laravel bao gồm phần mềm trung gian

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

60 và

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

61 trong ngăn xếp phần mềm trung gian toàn cầu của ứng dụng của bạn. Các phần mềm trung gian này được liệt kê trong ngăn xếp theo lớp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

62. Do đó, bạn sẽ thường cần đánh dấu các trường yêu cầu "tùy chọn" của mình là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

63 nếu bạn không muốn trình xác thực coi các giá trị

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 là không hợp lệ. Ví dụ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

3

Trong ví dụ này, chúng tôi chỉ định rằng trường

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

65 có thể là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 hoặc biểu diễn ngày hợp lệ. Nếu công cụ sửa đổi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

63 không được thêm vào định nghĩa quy tắc, trình xác thực sẽ coi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 là một ngày không hợp lệ

Định dạng phản hồi lỗi xác thực

Khi ứng dụng của bạn đưa ra một ngoại lệ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

29 và yêu cầu HTTP đến đang chờ phản hồi JSON, Laravel sẽ tự động định dạng thông báo lỗi cho bạn và trả về phản hồi HTTP

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

70

Dưới đây, bạn có thể xem lại một ví dụ về định dạng phản hồi JSON cho các lỗi xác thực. Lưu ý rằng các khóa lỗi lồng nhau được làm phẳng thành định dạng ký hiệu "dấu chấm"

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

4

Xác nhận yêu cầu biểu mẫu

Tạo yêu cầu biểu mẫu

Đối với các tình huống xác thực phức tạp hơn, bạn có thể muốn tạo một "yêu cầu biểu mẫu". Yêu cầu biểu mẫu là các lớp yêu cầu tùy chỉnh đóng gói logic xác thực và ủy quyền của riêng chúng. Để tạo một lớp yêu cầu biểu mẫu, bạn có thể sử dụng lệnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

71 Artisan CLI

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

5

Lớp yêu cầu biểu mẫu được tạo sẽ được đặt trong thư mục

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

72. Nếu thư mục này không tồn tại, nó sẽ được tạo khi bạn chạy lệnh

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

71. Mỗi yêu cầu biểu mẫu được tạo bởi Laravel có hai phương thức.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74 và

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

75

Như bạn có thể đoán, phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74 chịu trách nhiệm xác định xem người dùng hiện được xác thực có thể thực hiện hành động được yêu cầu thể hiện hay không, trong khi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

75 trả về các quy tắc xác thực sẽ áp dụng cho dữ liệu của yêu cầu

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

6

Lưu ý
Bạn có thể nhập gợi ý bất kỳ phụ thuộc nào bạn yêu cầu trong chữ ký của phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

75. Chúng sẽ tự động được giải quyết thông qua Laravel service container.

Vì vậy, các quy tắc xác nhận được đánh giá như thế nào? . Yêu cầu biểu mẫu đến được xác thực trước khi phương thức bộ điều khiển được gọi, nghĩa là bạn không cần phải làm lộn xộn bộ điều khiển của mình với bất kỳ logic xác thực nào

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

7

Nếu xác thực không thành công, phản hồi chuyển hướng sẽ được tạo để đưa người dùng trở lại vị trí trước đó của họ. Các lỗi cũng sẽ được flash vào phiên để chúng có sẵn để hiển thị. Nếu yêu cầu là yêu cầu XHR, phản hồi HTTP có mã trạng thái 422 sẽ được trả lại cho người dùng bao gồm biểu diễn JSON của các lỗi xác thực

Thêm After Hook vào Form Request

Nếu bạn muốn thêm móc xác thực "sau" vào yêu cầu biểu mẫu, bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

79. Phương thức này nhận được trình xác thực được xây dựng đầy đủ, cho phép bạn gọi bất kỳ phương thức nào của nó trước khi các quy tắc xác thực thực sự được đánh giá

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

8

Dừng trên thuộc tính lỗi xác thực đầu tiên

Bằng cách thêm thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

80 vào lớp yêu cầu của bạn, bạn có thể thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

9

Tùy chỉnh vị trí chuyển hướng

Như đã thảo luận trước đó, phản hồi chuyển hướng sẽ được tạo để gửi người dùng trở lại vị trí trước đó của họ khi xác thực yêu cầu biểu mẫu không thành công. Tuy nhiên, bạn có thể tự do tùy chỉnh hành vi này. Để làm như vậy, hãy xác định thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

81 theo yêu cầu biểu mẫu của bạn

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
70

Hoặc, nếu bạn muốn chuyển hướng người dùng đến một tuyến đường đã đặt tên, thay vào đó, bạn có thể xác định thuộc tính

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

82

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
71

Yêu cầu biểu mẫu ủy quyền

Lớp yêu cầu biểu mẫu cũng chứa một phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74. Trong phương pháp này, bạn có thể xác định xem người dùng được xác thực có thực sự có quyền cập nhật một tài nguyên nhất định hay không. Ví dụ: bạn có thể xác định xem người dùng có thực sự sở hữu nhận xét blog mà họ đang cố cập nhật hay không. Rất có thể, bạn sẽ tương tác với các cổng và chính sách ủy quyền của mình trong phương pháp này

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
72

Vì tất cả các yêu cầu biểu mẫu mở rộng lớp yêu cầu Laravel cơ sở, chúng tôi có thể sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

84 để truy cập người dùng hiện được xác thực. Ngoài ra, hãy lưu ý cuộc gọi đến phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

85 trong ví dụ trên. Phương thức này cấp cho bạn quyền truy cập vào các tham số URI được xác định trên tuyến đang được gọi, chẳng hạn như tham số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

86 trong ví dụ bên dưới

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
73

Do đó, nếu ứng dụng của bạn đang tận dụng lợi thế của liên kết mô hình định tuyến, mã của bạn có thể được thực hiện ngắn gọn hơn nữa bằng cách truy cập mô hình đã giải quyết dưới dạng thuộc tính của yêu cầu

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
74

Nếu phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74 trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88, phản hồi HTTP có mã trạng thái 403 sẽ tự động được trả về và phương thức điều khiển của bạn sẽ không thực thi

Nếu bạn định xử lý logic ủy quyền cho yêu cầu trong một phần khác của ứng dụng, bạn có thể chỉ cần trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89 từ phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
75

Lưu ý
Bạn có thể nhập gợi ý bất kỳ phụ thuộc nào bạn cần trong chữ ký của phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

74. Chúng sẽ tự động được giải quyết thông qua Laravel service container.

Customizing The Error Messages

Bạn có thể tùy chỉnh các thông báo lỗi được yêu cầu biểu mẫu sử dụng bằng cách ghi đè phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

92. Phương thức này sẽ trả về một mảng các cặp thuộc tính/quy tắc và các thông báo lỗi tương ứng của chúng

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
76

Tùy chỉnh các thuộc tính xác thực

Nhiều thông báo lỗi quy tắc xác thực tích hợp sẵn của Laravel chứa trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93. Nếu bạn muốn thay thế trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93 của thông báo xác thực bằng tên thuộc tính tùy chỉnh, bạn có thể chỉ định tên tùy chỉnh bằng cách ghi đè phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

95. Phương thức này sẽ trả về một mảng các cặp thuộc tính/tên

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
77

Chuẩn bị đầu vào để xác thực

Nếu bạn cần chuẩn bị hoặc làm sạch bất kỳ dữ liệu nào từ yêu cầu trước khi áp dụng các quy tắc xác thực của mình, bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

96

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
78

Tạo thủ công Trình xác thực

Nếu bạn không muốn sử dụng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 theo yêu cầu, bạn có thể tạo phiên bản trình xác thực theo cách thủ công bằng cách sử dụng mặt tiền

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

98. Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

99 trên mặt tiền tạo một phiên bản trình xác thực mới

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
79

Đối số đầu tiên được truyền cho phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

99 là dữ liệu đang được xác thực. Đối số thứ hai là một mảng các quy tắc xác thực sẽ được áp dụng cho dữ liệu

Sau khi xác định xem xác thực yêu cầu có thất bại hay không, bạn có thể sử dụng phương pháp

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
701 để flash thông báo lỗi vào phiên. Khi sử dụng phương pháp này, biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 sẽ tự động được chia sẻ với chế độ xem của bạn sau khi chuyển hướng, cho phép bạn dễ dàng hiển thị lại cho người dùng. Phương thức
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
701 chấp nhận trình xác thực,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
704 hoặc PHP
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705

Stopping On First Validation Failure

Phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

80 sẽ thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực

return back[]->with['error', 'The error message here!'];
00

Chuyển hướng tự động

Nếu bạn muốn tạo phiên bản trình xác thực theo cách thủ công nhưng vẫn tận dụng lợi thế của chuyển hướng tự động do phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 của yêu cầu HTTP cung cấp, bạn có thể gọi phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 trên phiên bản trình xác thực hiện có. Nếu xác thực không thành công, người dùng sẽ tự động được chuyển hướng hoặc trong trường hợp yêu cầu XHR, phản hồi JSON sẽ được trả về

return back[]->with['error', 'The error message here!'];
01

Bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

34 để lưu trữ thông báo lỗi trong túi lỗi được đặt tên nếu xác thực không thành công

return back[]->with['error', 'The error message here!'];
02

Túi lỗi được đặt tên

Nếu bạn có nhiều biểu mẫu trên một trang, bạn có thể đặt tên cho

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
704 chứa các lỗi xác thực, cho phép bạn truy xuất các thông báo lỗi cho một biểu mẫu cụ thể. Để đạt được điều này, hãy chuyển một tên làm đối số thứ hai cho
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
701

return back[]->with['error', 'The error message here!'];
03

Sau đó, bạn có thể truy cập phiên bản có tên

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
704 từ biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39

return back[]->with['error', 'The error message here!'];
04

Customizing The Error Messages

Nếu cần, bạn có thể cung cấp thông báo lỗi tùy chỉnh mà phiên bản trình xác thực nên sử dụng thay vì thông báo lỗi mặc định do Laravel cung cấp. Có một số cách để chỉ định thông báo tùy chỉnh. Trước tiên, bạn có thể chuyển các thông báo tùy chỉnh làm đối số thứ ba cho phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
714

return back[]->with['error', 'The error message here!'];
05

Trong ví dụ này, trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93 sẽ được thay thế bằng tên thực của trường được xác thực. Bạn cũng có thể sử dụng các trình giữ chỗ khác trong thông báo xác thực. Ví dụ

return back[]->with['error', 'The error message here!'];
06

Chỉ định một thông báo tùy chỉnh cho một thuộc tính nhất định

Đôi khi bạn có thể muốn chỉ định một thông báo lỗi tùy chỉnh chỉ cho một thuộc tính cụ thể. Bạn có thể làm như vậy bằng cách sử dụng ký hiệu "dấu chấm". Chỉ định tên của thuộc tính trước, sau đó là quy tắc

return back[]->with['error', 'The error message here!'];
07

Chỉ định giá trị thuộc tính tùy chỉnh

Nhiều thông báo lỗi tích hợp của Laravel bao gồm một trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93 được thay thế bằng tên của trường hoặc thuộc tính được xác thực. Để tùy chỉnh các giá trị được sử dụng để thay thế các trình giữ chỗ này cho các trường cụ thể, bạn có thể chuyển một mảng các thuộc tính tùy chỉnh làm đối số thứ tư cho phương thức
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
714

return back[]->with['error', 'The error message here!'];
08

Móc sau khi xác thực

Bạn cũng có thể đính kèm các cuộc gọi lại để chạy sau khi xác thực hoàn tất. Điều này cho phép bạn dễ dàng thực hiện xác thực thêm và thậm chí thêm nhiều thông báo lỗi hơn vào bộ sưu tập thông báo. Để bắt đầu, hãy gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
718 trên phiên bản trình xác thực

return back[]->with['error', 'The error message here!'];
09

Làm việc với đầu vào đã xác thực

Sau khi xác thực dữ liệu yêu cầu đến bằng yêu cầu biểu mẫu hoặc phiên bản trình xác thực được tạo thủ công, bạn có thể muốn truy xuất dữ liệu yêu cầu đến thực sự đã trải qua quá trình xác thực. Điều này có thể được thực hiện theo nhiều cách. Đầu tiên, bạn có thể gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 trên một yêu cầu biểu mẫu hoặc phiên bản trình xác thực. Phương thức này trả về một mảng dữ liệu đã được xác thực

return back[]->with['error', 'The error message here!'];
20

Ngoài ra, bạn có thể gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
720 trên một yêu cầu biểu mẫu hoặc phiên bản trình xác thực. Phương thức này trả về một thể hiện của
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
721. Đối tượng này hiển thị các phương thức
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
722,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
723 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
724 để truy xuất một tập hợp con của dữ liệu đã xác thực hoặc toàn bộ mảng dữ liệu đã xác thực

return back[]->with['error', 'The error message here!'];
21

Ngoài ra, phiên bản

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
721 có thể được lặp lại và truy cập giống như một mảng

return back[]->with['error', 'The error message here!'];
22

Nếu bạn muốn thêm các trường bổ sung vào dữ liệu đã được xác thực, bạn có thể gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
726

return back[]->with['error', 'The error message here!'];
23

Nếu bạn muốn truy xuất dữ liệu đã được xác thực dưới dạng một phiên bản bộ sưu tập, bạn có thể gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
727

return back[]->with['error', 'The error message here!'];
24

Làm việc với thông báo lỗi

Sau khi gọi phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
728 trên phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

98, bạn sẽ nhận được phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

45, phiên bản này có nhiều phương thức thuận tiện để làm việc với các thông báo lỗi. Biến

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

39 được tự động cung cấp cho tất cả các chế độ xem cũng là một thể hiện của lớp
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
704

Truy xuất thông báo lỗi đầu tiên cho một trường

Để truy xuất thông báo lỗi đầu tiên cho một trường nhất định, hãy sử dụng phương pháp

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
733

return back[]->with['error', 'The error message here!'];
25

Truy xuất tất cả các thông báo lỗi cho một trường

Nếu bạn cần truy xuất một mảng gồm tất cả các thông báo cho một trường nhất định, hãy sử dụng phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
734

return back[]->with['error', 'The error message here!'];
26

Nếu bạn đang xác thực trường dạng mảng, bạn có thể truy xuất tất cả thông báo cho từng phần tử mảng bằng cách sử dụng ký tự

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
735

return back[]->with['error', 'The error message here!'];
27

Truy xuất tất cả các thông báo lỗi cho tất cả các trường

Để truy xuất một mảng gồm tất cả các thông báo cho tất cả các trường, hãy sử dụng phương thức

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
724

return back[]->with['error', 'The error message here!'];
28

Xác định xem có tồn tại thông báo cho một trường hay không

Phương pháp

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
737 có thể được sử dụng để xác định xem có bất kỳ thông báo lỗi nào tồn tại đối với một trường nhất định hay không

return back[]->with['error', 'The error message here!'];
29

Chỉ định tin nhắn tùy chỉnh trong tệp ngôn ngữ

Laravel's built-in validation rules each have an error message that is located in your application's

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

47 file. Trong tệp này, bạn sẽ tìm thấy mục dịch cho từng quy tắc xác thực. Bạn có thể tự do thay đổi hoặc sửa đổi các thông báo này dựa trên nhu cầu của ứng dụng của bạn

Ngoài ra, bạn có thể sao chép tệp này sang thư mục ngôn ngữ dịch thuật khác để dịch các thông báo cho ngôn ngữ của ứng dụng của bạn. Để tìm hiểu thêm về bản địa hóa Laravel, hãy xem tài liệu bản địa hóa đầy đủ

Thông báo tùy chỉnh cho các thuộc tính cụ thể

Bạn có thể tùy chỉnh các thông báo lỗi được sử dụng cho các kết hợp quy tắc và thuộc tính được chỉ định trong các tệp ngôn ngữ xác thực của ứng dụng của bạn. Để làm như vậy, hãy thêm các tùy chỉnh tin nhắn của bạn vào mảng

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
739 trong tệp ngôn ngữ
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
740 của ứng dụng của bạn

return back[]->with['error', 'The error message here!'];
40

Chỉ định thuộc tính trong tệp ngôn ngữ

Nhiều thông báo lỗi tích hợp của Laravel bao gồm một trình giữ chỗ

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93 được thay thế bằng tên của trường hoặc thuộc tính đang được xác thực. Nếu bạn muốn thay thế phần

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

93 của thông báo xác thực bằng một giá trị tùy chỉnh, bạn có thể chỉ định tên thuộc tính tùy chỉnh trong mảng

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

95 của tệp ngôn ngữ
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
740 của mình

return back[]->with['error', 'The error message here!'];
41

Chỉ định giá trị trong tệp ngôn ngữ

Một số thông báo lỗi quy tắc xác thực tích hợp của Laravel chứa trình giữ chỗ

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
745 được thay thế bằng giá trị hiện tại của thuộc tính yêu cầu. Tuy nhiên, đôi khi bạn có thể cần thay thế phần
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
745 trong thông báo xác thực của mình bằng một biểu thị tùy chỉnh của giá trị. Ví dụ: hãy xem xét quy tắc sau đây chỉ định rằng số thẻ tín dụng là bắt buộc nếu
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
747 có giá trị là
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
748

return back[]->with['error', 'The error message here!'];
42

Nếu quy tắc xác thực này không thành công, nó sẽ tạo ra thông báo lỗi sau

return back[]->with['error', 'The error message here!'];
43

Thay vì hiển thị

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
748 dưới dạng giá trị loại thanh toán, bạn có thể chỉ định cách biểu thị giá trị thân thiện với người dùng hơn trong tệp ngôn ngữ
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
740 của mình bằng cách xác định mảng
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
751

Sau khi xác định giá trị này, quy tắc xác thực sẽ tạo ra thông báo lỗi sau

return back[]->with['error', 'The error message here!'];
44

Quy tắc xác thực có sẵn

Dưới đây là danh sách tất cả các quy tắc xác thực có sẵn và chức năng của chúng

Đã được chấp nhận

Trường được xác thực phải là

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
752,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
753,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
754 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89. Điều này hữu ích để xác thực việc chấp nhận "Điều khoản dịch vụ" hoặc các trường tương tự

accept_if. trường khác, giá trị,

Trường đang được xác thực phải là

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
752,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
753,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
754 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89 nếu một trường khác đang được xác thực bằng một giá trị đã chỉ định. Điều này hữu ích để xác thực việc chấp nhận "Điều khoản dịch vụ" hoặc các trường tương tự

active_url

Trường được xác thực phải có bản ghi A hoặc AAAA hợp lệ theo chức năng PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
760. Tên máy chủ của URL được cung cấp được trích xuất bằng cách sử dụng hàm PHP
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
761 trước khi được chuyển đến
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
760

sau đó. ngày tháng

Trường được xác thực phải là một giá trị sau một ngày nhất định. Ngày tháng sẽ được chuyển vào hàm PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763 để được chuyển đổi thành phiên bản
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
764 hợp lệ

return back[]->with['error', 'The error message here!'];
45

Thay vì chuyển một chuỗi ngày để được đánh giá bởi

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763, bạn có thể chỉ định một trường khác để so sánh với ngày

return back[]->with['error', 'The error message here!'];
46

after_or_equal. ngày tháng

Trường được xác thực phải là một giá trị sau hoặc bằng ngày đã cho. Để biết thêm thông tin, hãy xem quy tắc sau

chữ cái

Trường được xác thực phải hoàn toàn là ký tự chữ cái

alpha_dash

Trường được xác thực có thể có các ký tự chữ và số, cũng như dấu gạch ngang và dấu gạch dưới

alpha_num

Trường được xác thực phải hoàn toàn là ký tự chữ và số

mảng

Trường được xác thực phải là PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705

Khi các giá trị bổ sung được cung cấp cho quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705, mỗi khóa trong mảng đầu vào phải có trong danh sách các giá trị được cung cấp cho quy tắc. Trong ví dụ sau, khóa
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
768 trong mảng đầu vào không hợp lệ vì nó không có trong danh sách các giá trị được cung cấp cho quy tắc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705

return back[]->with['error', 'The error message here!'];
47

Nói chung, bạn phải luôn chỉ định các khóa mảng được phép có trong mảng của mình

ascii

Trường được xác thực phải hoàn toàn là các ký tự ASCII 7 bit

bảo lãnh

Ngừng chạy các quy tắc xác thực cho trường sau lần xác thực đầu tiên không thành công

Mặc dù quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

35 sẽ chỉ ngừng xác thực một trường cụ thể khi gặp phải lỗi xác thực, nhưng phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

80 sẽ thông báo cho trình xác thực rằng nó sẽ ngừng xác thực tất cả các thuộc tính sau khi xảy ra lỗi xác thực.

return back[]->with['error', 'The error message here!'];
00

trước. ngày tháng

Trường được xác thực phải là một giá trị trước ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763 để được chuyển đổi thành một phiên bản
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
764 hợp lệ. Ngoài ra, giống như quy tắc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
718, tên của một trường khác đang được xác thực có thể được cung cấp dưới dạng giá trị của
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
775

before_or_equal. ngày tháng

Trường được xác thực phải là một giá trị trước hoặc bằng ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763 để được chuyển đổi thành một phiên bản
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
764 hợp lệ. Ngoài ra, giống như quy tắc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
718, tên của một trường khác đang được xác thực có thể được cung cấp dưới dạng giá trị của
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
775

ở giữa. nhỏ nhất lớn nhất

Trường được xác thực phải có kích thước nằm trong khoảng từ tối thiểu đến tối đa đã cho [đã bao gồm]. Các chuỗi, số, mảng và tệp được đánh giá theo cách tương tự như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

boolean

Trường được xác thực phải có thể được truyền dưới dạng boolean. Đầu vào được chấp nhận là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89,

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
754,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
784,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
785 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
786

đã xác nhận

Trường được xác thực phải có trường phù hợp là

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
787. Ví dụ: nếu trường được xác thực là
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
788, thì trường
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
789 phù hợp phải có trong thông tin đầu vào

mật khẩu hiện tại

Trường được xác thực phải khớp với mật khẩu của người dùng được xác thực. Bạn có thể chỉ định một trình bảo vệ xác thực bằng cách sử dụng tham số đầu tiên của quy tắc

return back[]->with['error', 'The error message here!'];
49

ngày tháng

Trường được xác thực phải là một ngày hợp lệ, không liên quan theo hàm PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763

ngày_bằng. ngày tháng

Trường được xác thực phải bằng ngày đã cho. Các ngày sẽ được chuyển vào hàm PHP

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
763 để được chuyển đổi thành một phiên bản
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
764 hợp lệ

Định dạng ngày tháng. định dạng

Trường được xác thực phải khớp với định dạng đã cho. Bạn nên sử dụng

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
775 hoặc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
794 khi xác thực một trường, không phải cả hai. This validation rule supports all formats supported by PHP's DateTime class

số thập phân. nhỏ nhất lớn nhất

Trường được xác thực phải là số và phải chứa số vị trí thập phân đã chỉ định

return back[]->with['error', 'The error message here!'];
80

suy giảm

Trường được xác thực phải là

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
795,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
796,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
784 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88

bị từ chối_if. trường khác, giá trị,

Trường đang được xác thực phải là

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
795,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
796,
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
784 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88 nếu một trường khác đang được xác thực bằng một giá trị đã chỉ định

khác biệt. cánh đồng

Trường được xác thực phải có giá trị khác với trường

chữ số. giá trị

Số nguyên được xác thực phải có độ dài giá trị chính xác

chữ số_giữa. nhỏ nhất lớn nhất

Xác thực số nguyên phải có độ dài giữa tối thiểu và tối đa đã cho

kích thước

Tệp đang được xác thực phải là một hình ảnh đáp ứng các ràng buộc về kích thước như được chỉ định bởi các tham số của quy tắc

return back[]->with['error', 'The error message here!'];
81

ràng buộc có sẵn là. min_width, max_width, min_height, max_height, chiều rộng, chiều cao, tỷ lệ

Một ràng buộc tỷ lệ phải được biểu diễn bằng chiều rộng chia cho chiều cao. Điều này có thể được chỉ định bằng một phân số như

return back[]->with['error', 'The error message here!'];
003 hoặc một số float như
return back[]->with['error', 'The error message here!'];
004

return back[]->with['error', 'The error message here!'];
82

Vì quy tắc này yêu cầu một số đối số, bạn có thể sử dụng phương pháp

return back[]->with['error', 'The error message here!'];
005 để xây dựng quy tắc một cách trôi chảy

return back[]->with['error', 'The error message here!'];
83

riêng biệt

Khi xác thực mảng, trường được xác thực không được có bất kỳ giá trị trùng lặp nào

Theo mặc định, Distinct sử dụng phép so sánh biến lỏng lẻo. Để sử dụng phép so sánh nghiêm ngặt, bạn có thể thêm tham số

return back[]->with['error', 'The error message here!'];
006 vào định nghĩa quy tắc xác thực của mình

return back[]->with['error', 'The error message here!'];
84

Bạn có thể thêm

return back[]->with['error', 'The error message here!'];
007 vào các đối số của quy tắc xác thực để làm cho quy tắc bỏ qua sự khác biệt về cách viết hoa

return back[]->with['error', 'The error message here!'];
85

không_bắt_đầu_với. thực phẩm, thanh,

Trường được xác thực không được bắt đầu bằng một trong các giá trị đã cho

không_cuối_với. thực phẩm, thanh,

Trường được xác thực không được kết thúc bằng một trong các giá trị đã cho

e-mail

Trường được xác thực phải được định dạng là địa chỉ email. Quy tắc xác thực này sử dụng gói

return back[]->with['error', 'The error message here!'];
008 để xác thực địa chỉ email. Theo mặc định, trình xác thực
return back[]->with['error', 'The error message here!'];
009 được áp dụng nhưng bạn cũng có thể áp dụng các kiểu xác thực khác

Ví dụ trên sẽ áp dụng các xác thực của

return back[]->with['error', 'The error message here!'];
009 và
return back[]->with['error', 'The error message here!'];
011. Dưới đây là danh sách đầy đủ các kiểu xác thực mà bạn có thể áp dụng

Trình xác thực

return back[]->with['error', 'The error message here!'];
020, sử dụng hàm
return back[]->with['error', 'The error message here!'];
025 của PHP, đi kèm với Laravel và là hành vi xác thực email mặc định của Laravel trước phiên bản 5 của Laravel. 8

Cảnh báo
Trình xác thực

return back[]->with['error', 'The error message here!'];
016 và
return back[]->with['error', 'The error message here!'];
018 yêu cầu tiện ích mở rộng PHP
return back[]->with['error', 'The error message here!'];
028.

kết_với. thực phẩm, thanh,

Trường được xác thực phải kết thúc bằng một trong các giá trị đã cho

liệt kê

Quy tắc

return back[]->with['error', 'The error message here!'];
029 là quy tắc dựa trên lớp xác thực xem trường được xác thực có chứa giá trị enum hợp lệ hay không. Quy tắc
return back[]->with['error', 'The error message here!'];
029 chấp nhận tên của enum làm đối số hàm tạo duy nhất của nó

return back[]->with['error', 'The error message here!'];
86

Cảnh báo
Enums chỉ khả dụng trên PHP 8. 1+.

loại trừ

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719

loại trừ_if. trường khác, giá trị

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 nếu trường otherfield bằng giá trị

Nếu logic loại trừ có điều kiện phức tạp được yêu cầu, bạn có thể sử dụng phương pháp

return back[]->with['error', 'The error message here!'];
035. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi được cung cấp một bao đóng, bao đóng phải trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88 để cho biết liệu trường đang được xác thực có nên bị loại trừ hay không

return back[]->with['error', 'The error message here!'];
87

loại trừ_unless. trường khác, giá trị

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 trừ khi trường của trường khác bằng giá trị. Nếu giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 [
return back[]->with['error', 'The error message here!'];
041], trường được xác thực sẽ bị loại trừ trừ khi trường so sánh là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 hoặc trường so sánh bị thiếu trong dữ liệu yêu cầu

loại trừ_với. lĩnh vực khác

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 nếu có trường otherfield

loại trừ_không. lĩnh vực khác

Trường đang được xác thực sẽ bị loại trừ khỏi dữ liệu yêu cầu được trả về bởi các phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 nếu trường otherfield không có mặt

tồn tại. bảng, cột

Trường được xác thực phải tồn tại trong một bảng cơ sở dữ liệu nhất định

Cách sử dụng cơ bản của quy tắc tồn tại

Nếu tùy chọn

return back[]->with['error', 'The error message here!'];
047 không được chỉ định, tên trường sẽ được sử dụng. Vì vậy, trong trường hợp này, quy tắc sẽ xác thực rằng bảng cơ sở dữ liệu
return back[]->with['error', 'The error message here!'];
048 chứa bản ghi có giá trị cột
return back[]->with['error', 'The error message here!'];
049 khớp với giá trị thuộc tính
return back[]->with['error', 'The error message here!'];
049 của yêu cầu

Chỉ định tên cột tùy chỉnh

Bạn có thể chỉ định rõ ràng tên cột cơ sở dữ liệu sẽ được sử dụng theo quy tắc xác thực bằng cách đặt nó sau tên bảng cơ sở dữ liệu

return back[]->with['error', 'The error message here!'];
88

Đôi khi, bạn có thể cần chỉ định một kết nối cơ sở dữ liệu cụ thể sẽ được sử dụng cho truy vấn

return back[]->with['error', 'The error message here!'];
051. Bạn có thể thực hiện việc này bằng cách thêm tên kết nối vào tên bảng

return back[]->with['error', 'The error message here!'];
89

Thay vì chỉ định trực tiếp tên bảng, bạn có thể chỉ định mô hình Eloquent sẽ được sử dụng để xác định tên bảng

return back[]->with['error', 'The error message here!'];
90

Nếu bạn muốn tùy chỉnh truy vấn được thực thi theo quy tắc xác thực, bạn có thể sử dụng lớp

return back[]->with['error', 'The error message here!'];
052 để xác định quy tắc một cách trôi chảy. Trong ví dụ này, chúng tôi cũng sẽ chỉ định các quy tắc xác thực dưới dạng một mảng thay vì sử dụng ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33 để phân định chúng

return back[]->with['error', 'The error message here!'];
91

Bạn có thể chỉ định rõ ràng tên cột cơ sở dữ liệu sẽ được sử dụng bởi quy tắc

return back[]->with['error', 'The error message here!'];
051 được tạo bởi phương thức
return back[]->with['error', 'The error message here!'];
055 bằng cách cung cấp tên cột làm đối số thứ hai cho phương thức
return back[]->with['error', 'The error message here!'];
051

return back[]->with['error', 'The error message here!'];
92

tập tin

Trường được xác thực phải là tệp đã tải lên thành công

điền

Trường được xác thực không được để trống khi nó hiện diện

gt. cánh đồng

Trường được xác thực phải lớn hơn trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

gte. cánh đồng

Trường được xác thực phải lớn hơn hoặc bằng trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

hình ảnh

Tệp được xác thực phải là hình ảnh [jpg, jpeg, png, bmp, gif, svg hoặc webp]

Trong. thực phẩm, thanh,

Trường được xác thực phải được bao gồm trong danh sách giá trị đã cho. Vì quy tắc này thường yêu cầu bạn phải

return back[]->with['error', 'The error message here!'];
059 một mảng, phương pháp
return back[]->with['error', 'The error message here!'];
060 có thể được sử dụng để xây dựng quy tắc một cách trôi chảy

return back[]->with['error', 'The error message here!'];
93

Khi quy tắc

return back[]->with['error', 'The error message here!'];
061 được kết hợp với quy tắc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705, mỗi giá trị trong mảng đầu vào phải có mặt trong danh sách các giá trị được cung cấp cho quy tắc
return back[]->with['error', 'The error message here!'];
061. Trong ví dụ sau, mã sân bay
return back[]->with['error', 'The error message here!'];
064 trong mảng đầu vào không hợp lệ vì nó không có trong danh sách sân bay được cung cấp cho quy tắc
return back[]->with['error', 'The error message here!'];
061

return back[]->with['error', 'The error message here!'];
94

in_array. lĩnh vực khác. *

Trường được xác thực phải tồn tại trong các giá trị của trường khác

số nguyên

Trường được xác thực phải là số nguyên

Cảnh báo
Quy tắc xác thực này không xác minh rằng đầu vào thuộc loại biến "số nguyên", chỉ xác minh rằng đầu vào thuộc loại được chấp nhận bởi quy tắc

return back[]->with['error', 'The error message here!'];
066 của PHP. Nếu bạn cần xác thực đầu vào là một số, vui lòng sử dụng quy tắc này kết hợp với quy tắc xác thực
return back[]->with['error', 'The error message here!'];
067.

ip

Trường được xác thực phải là địa chỉ IP

ipv4

Trường được xác thực phải là địa chỉ IPv4

ipv6

Trường được xác thực phải là địa chỉ IPv6

json

Trường được xác thực phải là một chuỗi JSON hợp lệ

trung úy. cánh đồng

Trường được xác thực phải nhỏ hơn trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

lte. cánh đồng

Trường được xác thực phải nhỏ hơn hoặc bằng trường đã cho. Hai trường phải cùng loại. Các chuỗi, số, mảng và tệp được đánh giá bằng cách sử dụng các quy ước giống như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

chữ thường

Trường được xác thực phải là chữ thường

địa chỉ MAC

Trường được xác thực phải là địa chỉ MAC

tối đa. giá trị

Trường được xác thực phải nhỏ hơn hoặc bằng giá trị tối đa. Các chuỗi, số, mảng và tệp được đánh giá theo cách tương tự như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

max_digits. giá trị

Số nguyên được xác thực phải có độ dài giá trị tối đa

kịch câm. văn bản/đồng bằng,

Tệp được xác thực phải khớp với một trong các loại MIME đã cho

return back[]->with['error', 'The error message here!'];
95

Để xác định loại MIME của tệp đã tải lên, nội dung của tệp sẽ được đọc và khung sẽ cố gắng đoán loại MIME, loại này có thể khác với loại MIME do khách hàng cung cấp

kịch câm. thực phẩm, thanh,

Tệp đang được xác thực phải có loại MIME tương ứng với một trong các tiện ích mở rộng được liệt kê

Cách sử dụng cơ bản của quy tắc MIME

return back[]->with['error', 'The error message here!'];
96

Mặc dù bạn chỉ cần chỉ định phần mở rộng, quy tắc này thực sự xác thực loại MIME của tệp bằng cách đọc nội dung của tệp và đoán loại MIME của nó. Có thể tìm thấy danh sách đầy đủ các loại MIME và các tiện ích mở rộng tương ứng của chúng tại vị trí sau

https. //svn. apache. org/repos/asf/httpd/httpd/trunk/docs/conf/mime. các loại

tối thiểu. giá trị

Trường được xác thực phải có giá trị tối thiểu. Các chuỗi, số, mảng và tệp được đánh giá theo cách tương tự như quy tắc

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
780

min_digits. giá trị

Số nguyên được xác thực phải có độ dài giá trị tối thiểu

multiple_of. giá trị

Trường được xác thực phải là bội số của giá trị

Cảnh báo
Cần có phần mở rộng PHP

return back[]->with['error', 'The error message here!'];
072 để sử dụng quy tắc
return back[]->with['error', 'The error message here!'];
073.

not_in. thực phẩm, thanh,

Trường được xác thực không được bao gồm trong danh sách giá trị đã cho. Phương pháp

return back[]->with['error', 'The error message here!'];
074 có thể được sử dụng để xây dựng thành thạo quy tắc

return back[]->with['error', 'The error message here!'];
97

not_regex. mẫu

Trường được xác thực không được khớp với biểu thức chính quy đã cho

Trong nội bộ, quy tắc này sử dụng hàm PHP

return back[]->with['error', 'The error message here!'];
075. Mẫu được chỉ định phải tuân theo cùng định dạng theo yêu cầu của
return back[]->with['error', 'The error message here!'];
075 và do đó cũng bao gồm các dấu phân cách hợp lệ. Ví dụ.
return back[]->with['error', 'The error message here!'];
077

Cảnh báo
Khi sử dụng các mẫu

return back[]->with['error', 'The error message here!'];
078 /
return back[]->with['error', 'The error message here!'];
079, có thể cần chỉ định các quy tắc xác thực của bạn bằng cách sử dụng một mảng thay vì sử dụng các dấu phân cách

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33, đặc biệt nếu biểu thức chính quy chứa ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33.

vô giá trị

Trường được xác thực có thể là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59

con số

Trường được xác thực phải là số

mật khẩu mở khóa

Trường được xác thực phải khớp với mật khẩu của người dùng được xác thực

Cảnh báo
Quy tắc này đã được đổi tên thành

return back[]->with['error', 'The error message here!'];
083 với ý định xóa nó trong Laravel 9. Vui lòng sử dụng quy tắc Mật khẩu hiện tại để thay thế.

hiện nay

Trường được xác thực phải có trong dữ liệu đầu vào nhưng có thể để trống

Cấm

Trường được xác thực phải là một chuỗi trống hoặc không có

bị cấm_if. trường khác, giá trị,

Trường được xác thực phải là một chuỗi trống hoặc không có nếu trường otherfield bằng bất kỳ giá trị nào

Nếu logic cấm có điều kiện phức tạp được yêu cầu, bạn có thể sử dụng phương pháp

return back[]->with['error', 'The error message here!'];
084. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi được cung cấp một bao đóng, bao đóng phải trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88 để cho biết liệu trường đang được xác thực có nên bị cấm hay không

return back[]->with['error', 'The error message here!'];
98

bị cấm_unless. trường khác, giá trị,

Trường được xác thực phải là một chuỗi trống hoặc không có trừ khi trường otherfield bằng bất kỳ giá trị nào

cấm. lĩnh vực khác,

Nếu có trường đang xác thực, thì không có trường nào trong trường khác có thể có, ngay cả khi trống

biểu thức chính quy. mẫu

Trường được xác thực phải khớp với biểu thức chính quy đã cho

Trong nội bộ, quy tắc này sử dụng hàm PHP

return back[]->with['error', 'The error message here!'];
075. Mẫu được chỉ định phải tuân theo cùng định dạng theo yêu cầu của
return back[]->with['error', 'The error message here!'];
075 và do đó cũng bao gồm các dấu phân cách hợp lệ. Ví dụ.
return back[]->with['error', 'The error message here!'];
089

Cảnh báo
Khi sử dụng các mẫu

return back[]->with['error', 'The error message here!'];
078 /
return back[]->with['error', 'The error message here!'];
079, có thể cần chỉ định các quy tắc trong một mảng thay vì sử dụng các dấu phân cách

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33, đặc biệt nếu biểu thức chính quy chứa ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33.

cần thiết

Trường được xác thực phải có trong dữ liệu đầu vào và không trống. Một trường được coi là "trống" nếu một trong các điều kiện sau là đúng

bắt buộc_if. trường khác, giá trị,

Trường được xác thực phải có mặt và không trống nếu trường otherfield bằng bất kỳ giá trị nào

Nếu bạn muốn xây dựng một điều kiện phức tạp hơn cho quy tắc

return back[]->with['error', 'The error message here!'];
096, bạn có thể sử dụng phương pháp
return back[]->with['error', 'The error message here!'];
097. Phương pháp này chấp nhận một boolean hoặc một bao đóng. Khi thông qua một lần đóng, lần đóng sẽ trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89 hoặc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88 để cho biết trường được xác thực có bắt buộc không

return back[]->with['error', 'The error message here!'];
99

bắt buộc_unless. trường khác, giá trị,

Trường được xác thực phải có mặt và không trống trừ khi trường otherfield bằng bất kỳ giá trị nào. Điều này cũng có nghĩa là trường khác phải có trong dữ liệu yêu cầu trừ khi giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59. Nếu giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 [
return back[]->with['error', 'The error message here!'];
202], trường được xác thực sẽ được yêu cầu trừ khi trường so sánh là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59 hoặc trường so sánh bị thiếu trong dữ liệu yêu cầu

bắt buộc_với. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi có bất kỳ trường nào được chỉ định khác có mặt và không trống

required_with_all. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi tất cả các trường được chỉ định khác có mặt và không trống

bắt buộc_không. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi bất kỳ trường nào được chỉ định khác trống hoặc không có

bắt buộc_không_có_tất_cả. thực phẩm, thanh,

Trường được xác thực phải có mặt và không trống chỉ khi tất cả các trường được chỉ định khác trống hoặc không có

required_array_keys. thực phẩm, thanh,

Trường được xác thực phải là một mảng và phải chứa ít nhất các khóa đã chỉ định

như nhau. cánh đồng

Trường đã cho phải khớp với trường đang được xác thực

kích thước. giá trị

Trường được xác thực phải có kích thước khớp với giá trị đã cho. Đối với dữ liệu chuỗi, giá trị tương ứng với số ký tự. Đối với dữ liệu số, giá trị tương ứng với một giá trị số nguyên nhất định [thuộc tính cũng phải có quy tắc

return back[]->with['error', 'The error message here!'];
067 hoặc
return back[]->with['error', 'The error message here!'];
205]. Đối với một mảng, kích thước tương ứng với
return back[]->with['error', 'The error message here!'];
206 của mảng. Đối với tệp, kích thước tương ứng với kích thước tệp tính bằng kilobyte. Hãy xem xét một số ví dụ

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
80

bắt đầu với. thực phẩm, thanh,

Trường được xác thực phải bắt đầu bằng một trong các giá trị đã cho

chuỗi

Trường được xác thực phải là một chuỗi. Nếu bạn muốn cho phép trường cũng là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59, bạn nên gán quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

63 cho trường

Múi giờ

Trường được xác thực phải là mã định danh múi giờ hợp lệ theo hàm PHP

return back[]->with['error', 'The error message here!'];
209

duy nhất. bảng, cột

Trường được xác thực không được tồn tại trong bảng cơ sở dữ liệu đã cho

Chỉ định tên bảng / cột tùy chỉnh

Thay vì chỉ định trực tiếp tên bảng, bạn có thể chỉ định mô hình Eloquent sẽ được sử dụng để xác định tên bảng

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
81

Tùy chọn

return back[]->with['error', 'The error message here!'];
047 có thể được sử dụng để chỉ định cột cơ sở dữ liệu tương ứng của trường. Nếu tùy chọn
return back[]->with['error', 'The error message here!'];
047 không được chỉ định, tên của trường được xác thực sẽ được sử dụng

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
82

Chỉ định kết nối cơ sở dữ liệu tùy chỉnh

Đôi khi, bạn có thể cần đặt kết nối tùy chỉnh cho các truy vấn cơ sở dữ liệu do Trình xác thực thực hiện. Để thực hiện điều này, bạn có thể thêm tên kết nối vào tên bảng

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
83

Buộc một quy tắc duy nhất bỏ qua một ID đã cho

Đôi khi, bạn có thể muốn bỏ qua một ID nhất định trong quá trình xác thực duy nhất. Ví dụ: xem xét màn hình "cập nhật hồ sơ" bao gồm tên, địa chỉ email và vị trí của người dùng. Bạn có thể sẽ muốn xác minh rằng địa chỉ email là duy nhất. Tuy nhiên, nếu người dùng chỉ thay đổi trường tên chứ không phải trường email, bạn không muốn xảy ra lỗi xác thực vì người dùng đã là chủ sở hữu của địa chỉ email được đề cập

Để hướng dẫn trình xác nhận bỏ qua ID của người dùng, chúng tôi sẽ sử dụng lớp

return back[]->with['error', 'The error message here!'];
052 để xác định quy tắc một cách trôi chảy. Trong ví dụ này, chúng tôi cũng sẽ chỉ định các quy tắc xác thực dưới dạng một mảng thay vì sử dụng ký tự

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

33 để phân định các quy tắc

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
84

Cảnh báo
Bạn không bao giờ được chuyển bất kỳ đầu vào yêu cầu do người dùng kiểm soát nào vào phương thức

return back[]->with['error', 'The error message here!'];
214. Thay vào đó, bạn chỉ nên chuyển một ID duy nhất do hệ thống tạo, chẳng hạn như ID tăng tự động hoặc UUID từ một phiên bản mô hình Eloquent. Nếu không, ứng dụng của bạn sẽ dễ bị tấn công SQL injection.

Thay vì chuyển giá trị của khóa mô hình sang phương thức

return back[]->with['error', 'The error message here!'];
214, bạn cũng có thể chuyển toàn bộ phiên bản mô hình. Laravel sẽ tự động trích xuất khóa từ mô hình

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
85

Nếu bảng của bạn sử dụng tên cột khóa chính khác với

return back[]->with['error', 'The error message here!'];
216, bạn có thể chỉ định tên của cột khi gọi phương thức
return back[]->with['error', 'The error message here!'];
214

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
86

Theo mặc định, quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

36 sẽ kiểm tra tính duy nhất của cột khớp với tên của thuộc tính đang được xác thực. Tuy nhiên, bạn có thể chuyển một tên cột khác làm đối số thứ hai cho phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

36

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
87

Thêm mệnh đề bổ sung

Bạn có thể chỉ định các điều kiện truy vấn bổ sung bằng cách tùy chỉnh truy vấn bằng phương pháp

return back[]->with['error', 'The error message here!'];
220. Ví dụ: hãy thêm một điều kiện truy vấn để phạm vi truy vấn chỉ tìm kiếm các bản ghi có giá trị cột
return back[]->with['error', 'The error message here!'];
221 là
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
754

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
88

chữ hoa

Trường được xác thực phải là chữ hoa

url

Trường được xác thực phải là một URL hợp lệ

ulid

Trường được xác thực phải là Mã định danh có thể sắp xếp theo từ điển duy nhất toàn cầu [ULID] hợp lệ

uuid

Trường được xác thực phải là số nhận dạng duy nhất toàn cầu RFC 4122 [phiên bản 1, 3, 4 hoặc 5] hợp lệ [UUID]

Thêm quy tắc có điều kiện

Bỏ qua xác thực khi các trường có giá trị nhất định

Đôi khi, bạn có thể không muốn xác thực một trường nhất định nếu một trường khác có một giá trị nhất định. Bạn có thể thực hiện việc này bằng cách sử dụng quy tắc xác thực

return back[]->with['error', 'The error message here!'];
223. Trong ví dụ này, các trường
return back[]->with['error', 'The error message here!'];
224 và
return back[]->with['error', 'The error message here!'];
225 sẽ không được xác thực nếu trường
return back[]->with['error', 'The error message here!'];
226 có giá trị là

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

88

@if[session[]->has['error']]
    

{{ session[]->get['error'] }}

@endif
89

Ngoài ra, bạn có thể sử dụng quy tắc

return back[]->with['error', 'The error message here!'];
228 để không xác thực một trường nhất định trừ khi một trường khác có giá trị nhất định

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

50

Xác thực khi có mặt

Trong một số trường hợp, bạn có thể muốn chạy kiểm tra xác thực đối với một trường chỉ khi trường đó có trong dữ liệu đang được xác thực. Để nhanh chóng hoàn thành việc này, hãy thêm quy tắc

return back[]->with['error', 'The error message here!'];
229 vào danh sách quy tắc của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

51

Trong ví dụ trên, trường

return back[]->with['error', 'The error message here!'];
230 sẽ chỉ được xác thực nếu nó có trong mảng
return back[]->with['error', 'The error message here!'];
231

Lưu ý
Nếu bạn đang cố xác thực một trường phải luôn hiện diện nhưng có thể trống, hãy xem ghi chú này trên các trường tùy chọn.

Xác thực có điều kiện phức tạp

Đôi khi bạn có thể muốn thêm các quy tắc xác thực dựa trên logic điều kiện phức tạp hơn. Ví dụ: bạn có thể chỉ muốn yêu cầu một trường nhất định nếu trường khác có giá trị lớn hơn 100. Hoặc, bạn có thể cần hai trường để chỉ có một giá trị nhất định khi có trường khác. Thêm các quy tắc xác thực này không phải là một điều khó khăn. Đầu tiên, tạo một phiên bản

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

98 với các quy tắc tĩnh không bao giờ thay đổi của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

52

Giả sử ứng dụng web của chúng tôi dành cho người sưu tầm trò chơi. Nếu một nhà sưu tập trò chơi đăng ký với ứng dụng của chúng tôi và họ sở hữu hơn 100 trò chơi, chúng tôi muốn họ giải thích lý do tại sao họ sở hữu nhiều trò chơi như vậy. Ví dụ: có lẽ họ điều hành một cửa hàng bán lại trò chơi hoặc có thể họ chỉ thích sưu tập trò chơi. Để thêm yêu cầu này một cách có điều kiện, chúng ta có thể sử dụng phương thức

return back[]->with['error', 'The error message here!'];
229 trên trường hợp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

98

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

53

Đối số đầu tiên được truyền cho phương thức

return back[]->with['error', 'The error message here!'];
229 là tên của trường mà chúng tôi đang xác thực có điều kiện. Đối số thứ hai là danh sách các quy tắc chúng tôi muốn thêm. Nếu lệnh đóng được thông qua khi đối số thứ ba trả về

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

89, các quy tắc sẽ được thêm vào. Phương pháp này giúp dễ dàng xây dựng các xác thực có điều kiện phức tạp. Bạn thậm chí có thể thêm xác thực có điều kiện cho một số trường cùng một lúc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

54

Lưu ý
Tham số

return back[]->with['error', 'The error message here!'];
237 được truyền cho lần đóng của bạn sẽ là một phiên bản của
return back[]->with['error', 'The error message here!'];
238 và có thể được sử dụng để truy cập thông tin đầu vào và tệp của bạn đang được xác thực.

Xác thực mảng có điều kiện phức tạp

Đôi khi, bạn có thể muốn xác thực một trường dựa trên một trường khác trong cùng một mảng lồng nhau có chỉ mục mà bạn không biết. Trong những tình huống này, bạn có thể cho phép bao đóng của mình nhận đối số thứ hai sẽ là mục riêng lẻ hiện tại trong mảng đang được xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

55

Giống như tham số

return back[]->with['error', 'The error message here!'];
237 được truyền cho bao đóng, tham số
return back[]->with['error', 'The error message here!'];
240 là một thể hiện của
return back[]->with['error', 'The error message here!'];
238 khi dữ liệu thuộc tính là một mảng;

Xác thực mảng

Như đã thảo luận trong tài liệu về quy tắc xác thực

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705, quy tắc
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
705 chấp nhận danh sách các khóa mảng được phép. Nếu có bất kỳ khóa bổ sung nào trong mảng, quá trình xác thực sẽ không thành công

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

56

Nói chung, bạn phải luôn chỉ định các khóa mảng được phép có trong mảng của mình. Mặt khác, các phương thức

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

21 và
@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
719 của trình xác thực sẽ trả về tất cả dữ liệu đã xác thực, bao gồm mảng và tất cả các khóa của nó, ngay cả khi các khóa đó không được xác thực bởi các quy tắc xác thực mảng lồng nhau khác

Xác thực đầu vào mảng lồng nhau

Việc xác thực các trường nhập biểu mẫu dựa trên mảng lồng nhau không phải là một điều khó khăn. Bạn có thể sử dụng "ký hiệu dấu chấm" để xác thực các thuộc tính trong một mảng. Ví dụ: nếu yêu cầu HTTP đến chứa trường

return back[]->with['error', 'The error message here!'];
246, bạn có thể xác thực nó như vậy

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

57

Bạn cũng có thể xác thực từng phần tử của một mảng. Ví dụ: để xác thực rằng mỗi email trong trường nhập mảng nhất định là duy nhất, bạn có thể thực hiện như sau

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

58

Likewise, you may use the

@if[session['successmessage']]
  
     {{session['successmessage']}}
     
              ×
    
  
@endif
735 character when specifying custom validation messages in your language files, making it a breeze to use a single validation message for array based fields

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

59

Accessing Nested Array Data

Sometimes you may need to access the value for a given nested array element when assigning validation rules to the attribute. You may accomplish this using the

return back[]->with['error', 'The error message here!'];
248 method. The
return back[]->with['error', 'The error message here!'];
249 method accepts a closure that will be invoked for each iteration of the array attribute under validation and will receive the attribute's value and explicit, fully-expanded attribute name. The closure should return an array of rules to assign to the array element

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

00

Error Message Indexes & Positions

When validating arrays, you may want to reference the index or position of a particular item that failed validation within the error message displayed by your application. To accomplish this, you may include the

return back[]->with['error', 'The error message here!'];
250 and
return back[]->with['error', 'The error message here!'];
251 place-holders within your custom validation message

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

01

Given the example above, validation will fail and the user will be presented with the following error of "Please describe photo #2. "

Validating Files

Laravel provides a variety of validation rules that may be used to validate uploaded files, such as

return back[]->with['error', 'The error message here!'];
252,
return back[]->with['error', 'The error message here!'];
253,
return back[]->with['error', 'The error message here!'];
254, and

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

38. While you are free to specify these rules individually when validating files, Laravel also offers a fluent file validation rule builder that you may find convenient

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

02

If your application accepts images uploaded by your users, you may use the

return back[]->with['error', 'The error message here!'];
256 rule's
return back[]->with['error', 'The error message here!'];
253 constructor method to indicate that the uploaded file should be an image. In addition, the
return back[]->with['error', 'The error message here!'];
258 rule may be used to limit the dimensions of the image

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

03

Note
More information regarding validating image dimensions may be found in the dimension rule documentation.

File Types

Even though you only need to specify the extensions when invoking the

return back[]->with['error', 'The error message here!'];
259 method, this method actually validates the MIME type of the file by reading the file's contents and guessing its MIME type. A full listing of MIME types and their corresponding extensions may be found at the following location

https. //svn. apache. org/repos/asf/httpd/httpd/trunk/docs/conf/mime. các loại

Validating Passwords

To ensure that passwords have an adequate level of complexity, you may use Laravel's

return back[]->with['error', 'The error message here!'];
260 rule object

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

04

The

return back[]->with['error', 'The error message here!'];
260 rule object allows you to easily customize the password complexity requirements for your application, such as specifying that passwords require at least one letter, number, symbol, or characters with mixed casing

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

05

In addition, you may ensure that a password has not been compromised in a public password data breach leak using the

return back[]->with['error', 'The error message here!'];
262 method

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

06

Bên trong, đối tượng quy tắc

return back[]->with['error', 'The error message here!'];
260 sử dụng mô hình k-Anonymity để xác định xem mật khẩu có bị rò rỉ thông qua haveibeenpwned hay không. com mà không làm mất quyền riêng tư hoặc bảo mật của người dùng

Theo mặc định, nếu mật khẩu xuất hiện ít nhất một lần trong một lần rò rỉ dữ liệu, mật khẩu đó sẽ bị coi là bị xâm phạm. Bạn có thể tùy chỉnh ngưỡng này bằng đối số đầu tiên của phương thức

return back[]->with['error', 'The error message here!'];
262

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

07

Tất nhiên, bạn có thể xâu chuỗi tất cả các phương thức trong các ví dụ trên

Xác định quy tắc mật khẩu mặc định

Bạn có thể thấy thuận tiện khi chỉ định các quy tắc xác thực mặc định cho mật khẩu ở một vị trí duy nhất trong ứng dụng của mình. Bạn có thể dễ dàng thực hiện việc này bằng cách sử dụng phương thức

return back[]->with['error', 'The error message here!'];
265, phương thức chấp nhận đóng. Việc đóng cho phương thức
return back[]->with['error', 'The error message here!'];
266 sẽ trả về cấu hình mặc định của quy tắc Mật khẩu. Thông thường, quy tắc ________ 2266 nên được gọi trong phương thức ________ 2268 của một trong những nhà cung cấp dịch vụ ứng dụng của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

08

Sau đó, khi bạn muốn áp dụng các quy tắc mặc định cho một mật khẩu cụ thể đang được xác thực, bạn có thể gọi phương thức

return back[]->with['error', 'The error message here!'];
266 mà không có đối số

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

09

Đôi khi, bạn có thể muốn đính kèm các quy tắc xác thực bổ sung vào quy tắc xác thực mật khẩu mặc định của mình. Bạn có thể sử dụng phương pháp

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

75 để thực hiện điều này

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

10

Quy tắc xác thực tùy chỉnh

Sử dụng đối tượng quy tắc

Laravel cung cấp nhiều quy tắc xác thực hữu ích; . Một phương pháp đăng ký quy tắc xác thực tùy chỉnh là sử dụng các đối tượng quy tắc. Để tạo một đối tượng quy tắc mới, bạn có thể sử dụng lệnh Artisan

return back[]->with['error', 'The error message here!'];
271. Hãy sử dụng lệnh này để tạo quy tắc xác minh một chuỗi là chữ hoa. Laravel sẽ đặt quy tắc mới trong thư mục
return back[]->with['error', 'The error message here!'];
272. Nếu thư mục này không tồn tại, Laravel sẽ tạo nó khi bạn thực thi lệnh Artisan để tạo quy tắc của mình

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

11

Khi quy tắc đã được tạo, chúng tôi sẵn sàng xác định hành vi của nó. Một đối tượng quy tắc chứa một phương thức duy nhất.

return back[]->with['error', 'The error message here!'];
273. Phương thức này nhận tên thuộc tính, giá trị của nó và một cuộc gọi lại sẽ được gọi khi không thành công với thông báo lỗi xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

12

Khi quy tắc đã được xác định, bạn có thể đính kèm nó vào trình xác thực bằng cách chuyển một phiên bản của đối tượng quy tắc với các quy tắc xác thực khác của bạn

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

13

Dịch thông báo xác thực

Thay vì cung cấp thông báo lỗi theo nghĩa đen cho phần đóng

return back[]->with['error', 'The error message here!'];
274, bạn cũng có thể cung cấp khóa chuỗi dịch và hướng dẫn Laravel dịch thông báo lỗi

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

14

Nếu cần, bạn có thể cung cấp các thay thế trình giữ chỗ và ngôn ngữ ưa thích làm đối số thứ nhất và thứ hai cho phương thức

return back[]->with['error', 'The error message here!'];
275

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

15

Truy cập dữ liệu bổ sung

Nếu lớp quy tắc xác thực tùy chỉnh của bạn cần truy cập vào tất cả dữ liệu khác đang được xác thực, thì lớp quy tắc của bạn có thể triển khai giao diện

return back[]->with['error', 'The error message here!'];
276. Giao diện này yêu cầu lớp của bạn xác định phương thức
return back[]->with['error', 'The error message here!'];
277. Phương thức này sẽ tự động được gọi bởi Laravel [trước khi tiến hành xác thực] với tất cả dữ liệu đang được xác thực

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

16

Hoặc, nếu quy tắc xác thực của bạn yêu cầu quyền truy cập vào phiên bản trình xác thực đang thực hiện xác thực, thì bạn có thể triển khai giao diện

return back[]->with['error', 'The error message here!'];
278

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

17

Sử dụng Closures

Nếu bạn chỉ cần chức năng của quy tắc tùy chỉnh một lần trong ứng dụng của mình, thì bạn có thể sử dụng bao đóng thay vì đối tượng quy tắc. Việc đóng nhận tên của thuộc tính, giá trị của thuộc tính và gọi lại

return back[]->with['error', 'The error message here!'];
274 sẽ được gọi nếu xác thực không thành công

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

18

Quy tắc ngầm

Theo mặc định, khi một thuộc tính đang được xác thực không có hoặc chứa một chuỗi trống, thì các quy tắc xác thực thông thường, bao gồm các quy tắc tùy chỉnh, sẽ không được chạy. Ví dụ: quy tắc

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

36 sẽ không được chạy đối với một chuỗi trống

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PostController extends Controller

* Show the form to create a new blog post.

* @return \Illuminate\View\View

return view['post.create'];

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

public function store[Request $request]

// Validate and store the blog post...

19

Để quy tắc tùy chỉnh chạy ngay cả khi thuộc tính trống, quy tắc phải ngụ ý rằng thuộc tính là bắt buộc. Để tạo nhanh một đối tượng quy tắc ẩn mới, bạn có thể sử dụng lệnh Artisan

return back[]->with['error', 'The error message here!'];
271 với tùy chọn
return back[]->with['error', 'The error message here!'];
282

Làm cách nào để hiển thị thông báo lỗi từ bộ điều khiển để xem trong Laravel?

Làm cách nào để gửi thông báo lỗi từ chế độ xem Bộ điều khiển trong laravel? .
chuyển hướng trở lại[]->back[]->with['tin nhắn', 'NÓ HOẠT ĐỘNG. ' ];
Hiển thị thông báo nếu nó tồn tại
@if[session[]->has['tin nhắn']]
{{ phiên[]->nhận['tin nhắn'] }}
.

Làm cách nào để quay lại với thông báo lỗi trong Laravel?

Chỉ cần đặt thông báo flash và chuyển hướng trở lại từ chức năng điều khiển của bạn. session[]->flash['msg', 'Thao tác đã thực hiện thành công. '];

Làm cách nào để hiển thị lỗi trong Laravel?

Theo mặc định, Laravel framework có tính năng xử lý lỗi. Tùy chọn gỡ lỗi trong tệp cấu hình xác định lỗi hiển thị ở cuối người dùng . Thông thường, tệp cấu hình Laravel cho phép gỡ lỗi là config/app. php.

Làm cách nào để hiển thị thông báo lỗi trong Laravel 8?

Trong một lệnh @error, bạn có thể lặp lại biến $message để hiển thị thông báo lỗi. .

Chủ Đề