Lỗi not available in current platfom trong steam năm 2024
We've brought together everything from the developer, your friends, and the community into a single place to make it easier to keep up with what's happening outside the game. Show
Post Game Summary The post game summary appears at the top of your game details page and makes it easy to see your achievements, screenshots, and trading cards earned during your most recent play session. Friends Who Play Want to know who plays this game? Maybe you’re stuck and need some advice, trying to build a party, or just want to know you’re not alone in the universe – fear not your friends are here now. Friends' Posts Posts from friends including shared screenshots, achievements and status updates now appear in the game details feed. Leave a comment or like a post to keep the conversation going without leaving the library. Community Content Steam users are already creating amazing fan art, game guides, videos, and memes for some of your favorite games. We spared no pixels bringing that content to the bottom of your game page so you can easily see what’s new in the community and join the conversation. Laravel provides several different approaches to validate your application's incoming data. It is most common to use the
39 method available on all incoming HTTP requests. However, we will discuss other approaches to validation as well. Laravel includes a wide variety of convenient validation rules that you may apply to data, even providing the ability to validate if values are unique in a given database table. We'll cover each of these validation rules in detail so that you are familiar with all of Laravel's validation features. Validation QuickstartTo learn about Laravel's powerful validation features, let's look at a complete example of validating a form and displaying the error messages back to the user. By reading this high-level overview, you'll be able to gain a good general understanding of how to validate incoming request data using Laravel: Defining the RoutesFirst, let's assume we have the following routes defined in our
40 file:
The
41 route will display a form for the user to create a new blog post, while the
42 route will store the new blog post in the database. Creating the ControllerNext, let's take a look at a simple controller that handles incoming requests to these routes. We'll leave the
43 method empty for now:
Writing the Validation LogicNow we are ready to fill in our
43 method with the logic to validate the new blog post. To do this, we will use the
39 method provided by the
46 object. If the validation rules pass, your code will keep executing normally; however, if validation fails, an
47 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 will be returned. To get a better understanding of the
39 method, let's jump back into the
43 method:
As you can see, the validation rules are passed into the
39 method. Don't worry - all available validation rules are . 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
51 delimited string:
In addition, you may use the
52 method to validate a request and store any error messages within a :
Stopping on First Validation FailureSometimes you may wish to stop running validation rules on an attribute after the first validation failure. To do so, assign the
53 rule to the attribute:
In this example, if the
54 rule on the
55 attribute fails, the
56 rule will not be checked. Rules will be validated in the order they are assigned. A Note on Nested AttributesIf the incoming HTTP request contains "nested" field data, you may specify these fields in your validation rules using "dot" syntax:
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:
Displaying the Validation ErrorsSo, 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 will automatically be . An
57 variable is shared with all of your application's views by the
58 middleware, which is provided by the
59 middleware group. When this middleware is applied an
57 variable will always be available in your views, allowing you to conveniently assume the
57 variable is always defined and can be safely used. The
57 variable will be an instance of
63. For more information on working with this object, . So, in our example, the user will be redirected to our controller's
64 method when validation fails, allowing us to display the error messages in the view:
Customizing the Error MessagesLaravel's built-in validation rules each have an error message that is located in your application's
65 file. If your application does not have a
66 directory, you may instruct Laravel to create it using the
67 Artisan command. Within the
65 file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application. In addition, you may copy this file to another language directory to translate the messages for your application's language. To learn more about Laravel localization, check out the complete localization documentation. [!WARNING] XHR Requests and ValidationIn this example, we used a traditional form to send data to the application. However, many applications receive XHR requests from a JavaScript powered frontend. When using the
39 method during an XHR request, Laravel will not generate a redirect response. Instead, Laravel generates a . This JSON response will be sent with a 422 HTTP status code. The
72 Directive You may use the
72 Blade directive to quickly determine if validation error messages exist for a given attribute. Within an
72 directive, you may echo the
75 variable to display the error message:
If you are using , you may pass the name of the error bag as the second argument to the
72 directive:
0 Repopulating FormsWhen Laravel generates a redirect response due to a validation error, the framework will automatically . This is done so that you may conveniently access the input during the next request and repopulate the form that the user attempted to submit. To retrieve flashed input from the previous request, invoke the
77 method on an instance of
46. The
77 method will pull the previously flashed input data from the session:
1 Laravel also provides a global
77 helper. If you are displaying old input within a Blade template, it is more convenient to use the
77 helper to repopulate the form. If no old input exists for the given field,
82 will be returned:
2 A Note on Optional FieldsBy default, Laravel includes the
83 and
84 middleware in your application's global middleware stack. Because of this, you will often need to mark your "optional" request fields as
85 if you do not want the validator to consider
82 values as invalid. For example:
3 In this example, we are specifying that the
87 field may be either
82 or a valid date representation. If the
85 modifier is not added to the rule definition, the validator would consider
82 an invalid date. Validation Error Response FormatWhen your application throws a
47 exception and the incoming HTTP request is expecting a JSON response, Laravel will automatically format the error messages for you and return a
92 HTTP response. Below, you can review an example of the JSON response format for validation errors. Note that nested error keys are flattened into "dot" notation format:
4 Form Request ValidationCreating Form RequestsFor more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that encapsulate their own validation and authorization logic. To create a form request class, you may use the
93 Artisan CLI command:
5 The generated form request class will be placed in the
94 directory. If this directory does not exist, it will be created when you run the
93 command. Each form request generated by Laravel has two methods:
96 and
97. As you might have guessed, the
96 method is responsible for determining if the currently authenticated user can perform the action represented by the request, while the
97 method returns the validation rules that should apply to the request's data:
6 [!NOTE] So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:
7 If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an XHR request, an HTTP response with a 422 status code will be returned to the user including a . [!NOTE] Need to add real-time form request validation to your Inertia powered Laravel frontend? Check out Laravel Precognition. Performing Additional ValidationSometimes you need to perform additional validation after your initial validation is complete. You can accomplish this using the form request's
01 method. The
01 method should return an array of callables or closures which will be invoked after validation is complete. The given callables will receive an
03 instance, allowing you to raise additional error messages if necessary:
8 As noted, the array returned by the
01 method may also contain invokable classes. The
05 method of these classes will receive an
03 instance:
9 Stopping on the First Validation FailureBy adding a
07 property to your request class, you may inform the validator that it should stop validating all attributes once a single validation failure has occurred:
0 Customizing the Redirect LocationAs previously discussed, a redirect response will be generated to send the user back to their previous location when form request validation fails. However, you are free to customize this behavior. To do so, define a
08 property on your form request:
1 Or, if you would like to redirect users to a named route, you may define a
09 property instead:
2 Authorizing Form RequestsThe form request class also contains an
96 method. Within this method, you may determine if the authenticated user actually has the authority to update a given resource. For example, you may determine if a user actually owns a blog comment they are attempting to update. Most likely, you will interact with your authorization gates and policies within this method:
3 Since all form requests extend the base Laravel request class, we may use the
11 method to access the currently authenticated user. Also, note the call to the
12 method in the example above. This method grants you access to the URI parameters defined on the route being called, such as the
13 parameter in the example below:
4 Therefore, if your application is taking advantage of , your code may be made even more succinct by accessing the resolved model as a property of the request:
5 If the
96 method returns
15, an HTTP response with a 403 status code will automatically be returned and your controller method will not execute. If you plan to handle authorization logic for the request in another part of your application, you may remove the
96 method completely, or simply return
17:
6 [!NOTE] Customizing the Error MessagesYou may customize the error messages used by the form request by overriding the
19 method. This method should return an array of attribute / rule pairs and their corresponding error messages:
7 Customizing the Validation AttributesMany of Laravel's built-in validation rule error messages contain an
20 placeholder. If you would like the
20 placeholder of your validation message to be replaced with a custom attribute name, you may specify the custom names by overriding the
22 method. This method should return an array of attribute / name pairs:
8 Preparing Input for ValidationIf you need to prepare or sanitize any data from the request before you apply your validation rules, you may use the
23 method:
9 Likewise, if you need to normalize any request data after validation is complete, you may use the
24 method:
0 Manually Creating ValidatorsIf you do not want to use the
39 method on the request, you may create a validator instance manually using the
26 facade. The
27 method on the facade generates a new validator instance:
1 The first argument passed to the
27 method is the data under validation. The second argument is an array of the validation rules that should be applied to the data. After determining whether the request validation failed, you may use the
29 method to flash the error messages to the session. When using this method, the
57 variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user. The
29 method accepts a validator, a
32, or a PHP
33. Stopping on First Validation FailureThe
07 method will inform the validator that it should stop validating all attributes once a single validation failure has occurred:
2 Automatic RedirectionIf you would like to create a validator instance manually but still take advantage of the automatic redirection offered by the HTTP request's
39 method, you may call the
39 method on an existing validator instance. If validation fails, the user will automatically be redirected or, in the case of an XHR request, a :
3 You may use the
52 method to store the error messages in a if validation fails:
4 Named Error BagsIf you have multiple forms on a single page, you may wish to name the
32 containing the validation errors, allowing you to retrieve the error messages for a specific form. To achieve this, pass a name as the second argument to
29:
5 You may then access the named
32 instance from the
57 variable:
6 Customizing the Error MessagesIf needed, you may provide custom error messages that a validator instance should use instead of the default error messages provided by Laravel. There are several ways to specify custom messages. First, you may pass the custom messages as the third argument to the
42 method:
7 In this example, the
20 placeholder will be replaced by the actual name of the field under validation. You may also utilize other placeholders in validation messages. For example:
8 Specifying a Custom Message for a Given AttributeSometimes you may wish to specify a custom error message only for a specific attribute. You may do so using "dot" notation. Specify the attribute's name first, followed by the rule:
9 Specifying Custom Attribute ValuesMany of Laravel's built-in error messages include an
20 placeholder that is replaced with the name of the field or attribute under validation. To customize the values used to replace these placeholders for specific fields, you may pass an array of custom attributes as the fourth argument to the
42 method:
0 Performing Additional ValidationSometimes you need to perform additional validation after your initial validation is complete. You can accomplish this using the validator's
01 method. The
01 method accepts a closure or an array of callables which will be invoked after validation is complete. The given callables will receive an
03 instance, allowing you to raise additional error messages if necessary:
1 As noted, the
01 method also accepts an array of callables, which is particularly convenient if your "after validation" logic is encapsulated in invokable classes, which will receive an
03 instance via their
05 method:
2 Working With Validated InputAfter validating incoming request data using a form request or a manually created validator instance, you may wish to retrieve the incoming request data that actually underwent validation. This can be accomplished in several ways. First, you may call the
52 method on a form request or validator instance. This method returns an array of the data that was validated:
3 Alternatively, you may call the
53 method on a form request or validator instance. This method returns an instance of
54. This object exposes
55,
56, and
57 methods to retrieve a subset of the validated data or the entire array of validated data:
4 In addition, the
54 instance may be iterated over and accessed like an array:
5 If you would like to add additional fields to the validated data, you may call the
59 method:
6 If you would like to retrieve the validated data as a collection instance, you may call the
60 method:
7 Working With Error MessagesAfter calling the
61 method on a
26 instance, you will receive an
63 instance, which has a variety of convenient methods for working with error messages. The
57 variable that is automatically made available to all views is also an instance of the
32 class. Retrieving the First Error Message for a FieldTo retrieve the first error message for a given field, use the
66 method:
8 Retrieving All Error Messages for a FieldIf you need to retrieve an array of all the messages for a given field, use the
67 method:
9 If you are validating an array form field, you may retrieve all of the messages for each of the array elements using the
68 character:
0 Retrieving All Error Messages for All FieldsTo retrieve an array of all messages for all fields, use the
57 method:
1 Determining if Messages Exist for a FieldThe
70 method may be used to determine if any error messages exist for a given field:
2 Specifying Custom Messages in Language FilesLaravel's built-in validation rules each have an error message that is located in your application's
65 file. If your application does not have a
66 directory, you may instruct Laravel to create it using the
67 Artisan command. Within the
65 file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application. In addition, you may copy this file to another language directory to translate the messages for your application's language. To learn more about Laravel localization, check out the complete localization documentation. [!WARNING] Custom Messages for Specific AttributesYou may customize the error messages used for specified attribute and rule combinations within your application's validation language files. To do so, add your message customizations to the
77 array of your application's
78 language file:
3 Specifying Attributes in Language FilesMany of Laravel's built-in error messages include an
20 placeholder that is replaced with the name of the field or attribute under validation. If you would like the
20 portion of your validation message to be replaced with a custom value, you may specify the custom attribute name in the
22 array of your
78 language file:
4 [!WARNING] Specifying Values in Language FilesSome of Laravel's built-in validation rule error messages contain a
85 placeholder that is replaced with the current value of the request attribute. However, you may occasionally need the
85 portion of your validation message to be replaced with a custom representation of the value. For example, consider the following rule that specifies that a credit card number is required if the
87 has a value of
88:
5 If this validation rule fails, it will produce the following error message:
6 Instead of displaying
88 as the payment type value, you may specify a more user-friendly value representation in your
78 language file by defining a
91 array:
7 [!WARNING] After defining this value, the validation rule will produce the following error message:
8 Available Validation RulesBelow is a list of all available validation rules and their function: acceptedThe field under validation must be
94,
95,
96,
97,
17, or
99. This is useful for validating "Terms of Service" acceptance or similar fields. accepted_if:anotherfield,value,...The field under validation must be
94,
95,
96,
97,
17, or
99 if another field under validation is equal to a specified value. This is useful for validating "Terms of Service" acceptance or similar fields. active_urlThe field under validation must have a valid A or AAAA record according to the
06 PHP function. The hostname of the provided URL is extracted using the
07 PHP function before being passed to
06. after:dateThe field under validation must be a value after a given date. The dates will be passed into the
09 PHP function in order to be converted to a valid
10 instance:
9 Instead of passing a date string to be evaluated by
09, you may specify another field to compare against the date:
0 after_or_equal:dateThe field under validation must be a value after or equal to the given date. For more information, see the rule. alphaThe field under validation must be entirely Unicode alphabetic characters contained in
12 and
13. To restrict this validation rule to characters in the ASCII range (
14 and
15), you may provide the
16 option to the validation rule:
1 alpha_dashThe field under validation must be entirely Unicode alpha-numeric characters contained in
12,
13,
19, as well as ASCII dashes (
21). To restrict this validation rule to characters in the ASCII range (
14 and
15), you may provide the
16 option to the validation rule:
2 alpha_numThe field under validation must be entirely Unicode alpha-numeric characters contained in
12,
13, and
19. To restrict this validation rule to characters in the ASCII range (
14 and
15), you may provide the
16 option to the validation rule:
3 arrayThe field under validation must be a PHP
33. When additional values are provided to the
33 rule, each key in the input array must be present within the list of values provided to the rule. In the following example, the
33 key in the input array is invalid since it is not contained in the list of values provided to the
33 rule:
4 In general, you should always specify the array keys that are allowed to be present within your array. asciiThe field under validation must be entirely 7-bit ASCII characters. bailStop running validation rules for the field after the first validation failure. While the
53 rule will only stop validating a specific field when it encounters a validation failure, the
07 method will inform the validator that it should stop validating all attributes once a single validation failure has occurred:
2 before:dateThe field under validation must be a value preceding the given date. The dates will be passed into the PHP
09 function in order to be converted into a valid
10 instance. In addition, like the rule, the name of another field under validation may be supplied as the value of
40. before_or_equal:dateThe field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP
09 function in order to be converted into a valid
10 instance. In addition, like the rule, the name of another field under validation may be supplied as the value of
40. between:min,maxThe field under validation must have a size between the given min and max (inclusive). Strings, numerics, arrays, and files are evaluated in the same fashion as the rule. booleanThe field under validation must be able to be cast as a boolean. Accepted input are
17,
15,
96,
49,
97, and
51. confirmedThe field under validation must have a matching field of
52. For example, if the field under validation is
53, a matching
54 field must be present in the input. current_passwordThe field under validation must match the authenticated user's password. You may specify an authentication guard using the rule's first parameter:
6 dateThe field under validation must be a valid, non-relative date according to the
09 PHP function. date_equals:dateThe field under validation must be equal to the given date. The dates will be passed into the PHP
09 function in order to be converted into a valid
10 instance. date_format:format,...The field under validation must match one of the given formats. You should use either
40 or
59 when validating a field, not both. This validation rule supports all formats supported by PHP's DateTime class. decimal:min,maxThe field under validation must be numeric and must contain the specified number of decimal places:
7 declinedThe field under validation must be
60,
61,
49,
51,
15, or
65. declined_if:anotherfield,value,...The field under validation must be
60,
61,
49,
51,
15, or
65 if another field under validation is equal to a specified value. different:fieldThe field under validation must have a different value than field. digits:valueThe integer under validation must have an exact length of value. digits_between:min,maxThe integer validation must have a length between the given min and max. dimensionsThe file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
8 Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio. A ratio constraint should be represented as width divided by height. This can be specified either by a fraction like
72 or a float like
73:
9 Since this rule requires several arguments, you may use the
74 method to fluently construct the rule:
0 distinctWhen validating arrays, the field under validation must not have any duplicate values:
1 Distinct uses loose variable comparisons by default. To use strict comparisons, you may add the
75 parameter to your validation rule definition:
2 You may add
76 to the validation rule's arguments to make the rule ignore capitalization differences:
3 doesnt_start_with:foo,bar,...The field under validation must not start with one of the given values. doesnt_end_with:foo,bar,...The field under validation must not end with one of the given values. The field under validation must be formatted as an email address. This validation rule utilizes the
77 package for validating the email address. By default, the
78 validator is applied, but you can apply other validation styles as well:
4 The example above will apply the
78 and
80 validations. Here's a full list of validation styles you can apply:
The
89 validator, which uses PHP's
94 function, ships with Laravel and was Laravel's default email validation behavior prior to Laravel version 5.8. [!WARNING] ends_with:foo,bar,...The field under validation must end with one of the given values. enumThe
98 rule is a class based rule that validates whether the field under validation contains a valid enum value. The
98 rule accepts the name of the enum as its only constructor argument. When validating primitive values, a backed Enum should be provided to the
98 rule:
5 The
98 rule's
55 and
56 methods may be used to limit which enum cases should be considered valid:
6 The
04 method may be used to conditionally modify the
98 rule:
7 excludeThe field under validation will be excluded from the request data returned by the
39 and
52 methods. exclude_if:anotherfield,valueThe field under validation will be excluded from the request data returned by the
39 and
52 methods if the anotherfield field is equal to value. If complex conditional exclusion logic is required, you may utilize the
10 method. This method accepts a boolean or a closure. When given a closure, the closure should return
17 or
15 to indicate if the field under validation should be excluded:
8 exclude_unless:anotherfield,valueThe field under validation will be excluded from the request data returned by the
39 and
52 methods unless anotherfield's field is equal to value. If value is
82 (
16), the field under validation will be excluded unless the comparison field is
82 or the comparison field is missing from the request data. exclude_with:anotherfieldThe field under validation will be excluded from the request data returned by the
39 and
52 methods if the anotherfield field is present. exclude_without:anotherfieldThe field under validation will be excluded from the request data returned by the
39 and
52 methods if the anotherfield field is not present. exists:table,columnThe field under validation must exist in a given database table. Basic Usage of Exists Rule
9 If the
22 option is not specified, the field name will be used. So, in this case, the rule will validate that the
23 database table contains a record with a
24 column value matching the request's
24 attribute value. Specifying a Custom Column NameYou may explicitly specify the database column name that should be used by the validation rule by placing it after the database table name:
0 Occasionally, you may need to specify a specific database connection to be used for the
26 query. You can accomplish this by prepending the connection name to the table name:
1 Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:
2 If you would like to customize the query executed by the validation rule, you may use the
27 class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the
51 character to delimit them:
3 You may explicitly specify the database column name that should be used by the
26 rule generated by the
30 method by providing the column name as the second argument to the
26 method:
4 extensions:foo,bar,...The file under validation must have a user-assigned extension corresponding to one of the listed extensions:
5 [!WARNING] You should never rely on validating a file by its user-assigned extension alone. This rule should typically always be used in combination with the or rules. fileThe field under validation must be a successfully uploaded file. filledThe field under validation must not be empty when it is present. gt:fieldThe field under validation must be greater than the given field or value. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule. gte:fieldThe field under validation must be greater than or equal to the given field or value. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule. hex_colorThe field under validation must contain a valid color value in hexadecimal format. imageThe file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp). in:foo,bar,...The field under validation must be included in the given list of values. Since this rule often requires you to
36 an array, the
37 method may be used to fluently construct the rule:
6 When the
38 rule is combined with the
33 rule, each value in the input array must be present within the list of values provided to the
38 rule. In the following example, the
41 airport code in the input array is invalid since it is not contained in the list of airports provided to the
38 rule:
7 in_array:anotherfield.*The field under validation must exist in anotherfield's values. integerThe field under validation must be an integer. [!WARNING] ipThe field under validation must be an IP address. ipv4The field under validation must be an IPv4 address. ipv6The field under validation must be an IPv6 address. jsonThe field under validation must be a valid JSON string. lt:fieldThe field under validation must be less than the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule. lte:fieldThe field under validation must be less than or equal to the given field. The two fields must be of the same type. Strings, numerics, arrays, and files are evaluated using the same conventions as the rule. lowercaseThe field under validation must be lowercase. listThe field under validation must be an array that is a list. An array is considered a list if its keys consist of consecutive numbers from 0 to
47. mac_addressThe field under validation must be a MAC address. max:valueThe field under validation must be less than or equal to a maximum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the rule. max_digits:valueThe integer under validation must have a maximum length of value. mimetypes:text/plain,...The file under validation must match one of the given MIME types:
8 To determine the MIME type of the uploaded file, the file's contents will be read and the framework will attempt to guess the MIME type, which may be different from the client's provided MIME type. mimes:foo,bar,...The file under validation must have a MIME type corresponding to one of the listed extensions:
9 Even though you only need to specify the extensions, this rule 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.types MIME Types and ExtensionsThis validation rule does not verify agreement between the MIME type and the extension the user assigned to the file. For example, the
49 validation rule would consider a file containing valid PNG content to be a valid PNG image, even if the file is named
50. If you would like to validate the user-assigned extension of the file, you may use the rule. min:valueThe field under validation must have a minimum value. Strings, numerics, arrays, and files are evaluated in the same fashion as the rule. min_digits:valueThe integer under validation must have a minimum length of value. multiple_of:valueThe field under validation must be a multiple of value. missingThe field under validation must not be present in the input data. missing_if:anotherfield,value,...The field under validation must not be present if the anotherfield field is equal to any value. missing_unless:anotherfield,valueThe field under validation must not be present unless the anotherfield field is equal to any value. missing_with:foo,bar,...The field under validation must not be present only if any of the other specified fields are present. missing_with_all:foo,bar,...The field under validation must not be present only if all of the other specified fields are present. not_in:foo,bar,...The field under validation must not be included in the given list of values. The
53 method may be used to fluently construct the rule:
0 not_regex:patternThe field under validation must not match the given regular expression. Internally, this rule uses the PHP
54 function. The pattern specified should obey the same formatting required by
54 and thus also include valid delimiters. For example:
56. [!WARNING] nullableThe field under validation may be
82. numericThe field under validation must be numeric. presentThe field under validation must exist in the input data. present_if:anotherfield,value,...The field under validation must be present if the anotherfield field is equal to any value. present_unless:anotherfield,valueThe field under validation must be present unless the anotherfield field is equal to any value. present_with:foo,bar,...The field under validation must be present only if any of the other specified fields are present. present_with_all:foo,bar,...The field under validation must be present only if all of the other specified fields are present. prohibitedThe field under validation must be missing or empty. A field is "empty" if it meets one of the following criteria:
prohibited_if:anotherfield,value,...The field under validation must be missing or empty if the anotherfield field is equal to any value. A field is "empty" if it meets one of the following criteria:
If complex conditional prohibition logic is required, you may utilize the
66 method. This method accepts a boolean or a closure. When given a closure, the closure should return
17 or
15 to indicate if the field under validation should be prohibited:
1 prohibited_unless:anotherfield,value,...The field under validation must be missing or empty unless the anotherfield field is equal to any value. A field is "empty" if it meets one of the following criteria:
prohibits:anotherfield,...If the field under validation is not missing or empty, all fields in anotherfield must be missing or empty. A field is "empty" if it meets one of the following criteria:
regex:patternThe field under validation must match the given regular expression. Internally, this rule uses the PHP
54 function. The pattern specified should obey the same formatting required by
54 and thus also include valid delimiters. For example:
75. [!WARNING] requiredThe field under validation must be present in the input data and not empty. A field is "empty" if it meets one of the following criteria:
required_if:anotherfield,value,...The field under validation must be present and not empty if the anotherfield field is equal to any value. If you would like to construct a more complex condition for the
82 rule, you may use the
83 method. This method accepts a boolean or a closure. When passed a closure, the closure should return
17 or
15 to indicate if the field under validation is required:
2 required_if_accepted:anotherfield,...The field under validation must be present and not empty if the anotherfield field is equal to
94,
95,
96,
97,
17, or
99. required_unless:anotherfield,value,...The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is
82. If value is
82 (
94), the field under validation will be required unless the comparison field is
82 or the comparison field is missing from the request data. required_with:foo,bar,...The field under validation must be present and not empty only if any of the other specified fields are present and not empty. required_with_all:foo,bar,...The field under validation must be present and not empty only if all of the other specified fields are present and not empty. required_without:foo,bar,...The field under validation must be present and not empty only when any of the other specified fields are empty or not present. required_without_all:foo,bar,...The field under validation must be present and not empty only when all of the other specified fields are empty or not present. required_array_keys:foo,bar,...The field under validation must be an array and must contain at least the specified keys. same:fieldThe given field must match the field under validation. size:valueThe field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the
44 or
97 rule). For an array, size corresponds to the
98 of the array. For files, size corresponds to the file size in kilobytes. Let's look at some examples:
3 starts_with:foo,bar,...The field under validation must start with one of the given values. stringThe field under validation must be a string. If you would like to allow the field to also be
82, you should assign the
85 rule to the field. timezoneThe field under validation must be a valid timezone identifier according to the
01 method. The arguments accepted by the
01 method may also be provided to this validation rule:
4 unique:table,columnThe field under validation must not exist within the given database table. Specifying a Custom Table / Column Name: Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:
5 The
22 option may be used to specify the field's corresponding database column. If the
22 option is not specified, the name of the field under validation will be used.
6 Specifying a Custom Database Connection Occasionally, you may need to set a custom connection for database queries made by the Validator. To accomplish this, you may prepend the connection name to the table name:
7 Forcing a Unique Rule to Ignore a Given ID: Sometimes, you may wish to ignore a given ID during unique validation. For example, consider an "update profile" screen that includes the user's name, email address, and location. You will probably want to verify that the email address is unique. However, if the user only changes the name field and not the email field, you do not want a validation error to be thrown because the user is already the owner of the email address in question. To instruct the validator to ignore the user's ID, we'll use the
27 class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the
51 character to delimit the rules:
8 [!WARNING] Instead of passing the model key's value to the
07 method, you may also pass the entire model instance. Laravel will automatically extract the key from the model:
9 If your table uses a primary key column name other than
09, you may specify the name of the column when calling the
07 method:
00 By default, the
54 rule will check the uniqueness of the column matching the name of the attribute being validated. However, you may pass a different column name as the second argument to the
54 method:
01 Adding Additional Where Clauses: You may specify additional query conditions by customizing the query using the
13 method. For example, let's add a query condition that scopes the query to only search records that have an
14 column value of
96:
02 uppercaseThe field under validation must be uppercase. urlThe field under validation must be a valid URL. If you would like to specify the URL protocols that should be considered valid, you may pass the protocols as validation rule parameters:
03 ulidThe field under validation must be a valid Universally Unique Lexicographically Sortable Identifier (ULID). uuidThe field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID). Conditionally Adding RulesSkipping Validation When Fields Have Certain ValuesYou may occasionally wish to not validate a given field if another field has a given value. You may accomplish this using the
16 validation rule. In this example, the
17 and
18 fields will not be validated if the
19 field has a value of
15:
04 Alternatively, you may use the
21 rule to not validate a given field unless another field has a given value:
05 Validating When PresentIn some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the
22 rule to your rule list:
06 In the example above, the
23 field will only be validated if it is present in the
24 array. [!NOTE] If you are attempting to validate a field that should always be present but may be empty, check out . Complex Conditional ValidationSometimes you may wish to add validation rules based on more complex conditional logic. For example, you may wish to require a given field only if another field has a greater value than 100. Or, you may need two fields to have a given value only when another field is present. Adding these validation rules doesn't have to be a pain. First, create a
26 instance with your static rules that never change:
07 Let's assume our web application is for game collectors. If a game collector registers with our application and they own more than 100 games, we want them to explain why they own so many games. For example, perhaps they run a game resale shop, or maybe they just enjoy collecting games. To conditionally add this requirement, we can use the
22 method on the
26 instance.
08 The first argument passed to the
22 method is the name of the field we are conditionally validating. The second argument is a list of the rules we want to add. If the closure passed as the third argument returns
17, the rules will be added. This method makes it a breeze to build complex conditional validations. You may even add conditional validations for several fields at once:
09 [!NOTE] Complex Conditional Array ValidationSometimes you may want to validate a field based on another field in the same nested array whose index you do not know. In these situations, you may allow your closure to receive a second argument which will be the current individual item in the array being validated:
10 Like the
30 parameter passed to the closure, the
33 parameter is an instance of
31 when the attribute data is an array; otherwise, it is a string. Validating ArraysAs discussed in the , the
33 rule accepts a list of allowed array keys. If any additional keys are present within the array, validation will fail:
4 In general, you should always specify the array keys that are allowed to be present within your array. Otherwise, the validator's
39 and
52 methods will return all of the validated data, including the array and all of its keys, even if those keys were not validated by other nested array validation rules. Validating Nested Array InputValidating nested array based form input fields doesn't have to be a pain. You may use "dot notation" to validate attributes within an array. For example, if the incoming HTTP request contains a
39 field, you may validate it like so:
12 You may also validate each element of an array. For example, to validate that each email in a given array input field is unique, you may do the following:
13 Likewise, you may use the
68 character when specifying , making it a breeze to use a single validation message for array based fields:
14 Accessing Nested Array DataSometimes 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
41 method. The
42 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:
15 Error Message Indexes and PositionsWhen 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
43 (starts from
45 (starts from
16 Given the example above, validation will fail and the user will be presented with the following error of "Please describe photo 2."If necessary, you may reference more deeply nested indexes and positions via
47,
48,
49,
50, etc.
17 Validating FilesLaravel provides a variety of validation rules that may be used to validate uploaded files, such as
32,
52,
53, and
56. 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:
18 If your application accepts images uploaded by your users, you may use the
55 rule's
52 constructor method to indicate that the uploaded file should be an image. In addition, the
57 rule may be used to limit the dimensions of the image:
19 [!NOTE] More information regarding validating image dimensions may be found in the . File SizesFor convenience, minimum and maximum file sizes may be specified as a string with a suffix indicating the file size units. The
58,
59,
60, and
61 suffixes are supported:
20 File TypesEven though you only need to specify the extensions when invoking the
62 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.types Validating PasswordsTo ensure that passwords have an adequate level of complexity, you may use Laravel's
63 rule object:
21 The
63 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:
22 In addition, you may ensure that a password has not been compromised in a public password data breach leak using the
65 method:
23 Internally, the
63 rule object uses the k-Anonymity model to determine if a password has been leaked via the haveibeenpwned.com service without sacrificing the user's privacy or security. By default, if a password appears at least once in a data leak, it will be considered compromised. You can customize this threshold using the first argument of the
65 method:
24 Of course, you may chain all the methods in the examples above:
25 Defining Default Password RulesYou may find it convenient to specify the default validation rules for passwords in a single location of your application. You can easily accomplish this using the
68 method, which accepts a closure. The closure given to the
69 method should return the default configuration of the Password rule. Typically, the
69 rule should be called within the
71 method of one of your application's service providers:
26 Then, when you would like to apply the default rules to a particular password undergoing validation, you may invoke the
69 method with no arguments:
27 Occasionally, you may want to attach additional validation rules to your default password validation rules. You may use the
97 method to accomplish this:
28 Custom Validation RulesUsing Rule ObjectsLaravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the
74 Artisan command. Let's use this command to generate a rule that verifies a string is uppercase. Laravel will place the new rule in the
75 directory. If this directory does not exist, Laravel will create it when you execute the Artisan command to create your rule:
29 Once the rule has been created, we are ready to define its behavior. A rule object contains a single method:
39. This method receives the attribute name, its value, and a callback that should be invoked on failure with the validation error message:
30 Once the rule has been defined, you may attach it to a validator by passing an instance of the rule object with your other validation rules:
31 Translating Validation MessagesInstead of providing a literal error message to the
77 closure, you may also provide a translation string key and instruct Laravel to translate the error message:
32 If necessary, you may provide placeholder replacements and the preferred language as the first and second arguments to the
78 method:
33 Accessing Additional DataIf your custom validation rule class needs to access all of the other data undergoing validation, your rule class may implement the
79 interface. This interface requires your class to define a
80 method. This method will automatically be invoked by Laravel (before validation proceeds) with all of the data under validation:
34 Or, if your validation rule requires access to the validator instance performing the validation, you may implement the
81 interface:
35 Using ClosuresIf you only need the functionality of a custom rule once throughout your application, you may use a closure instead of a rule object. The closure receives the attribute's name, the attribute's value, and a
77 callback that should be called if validation fails:
36 Implicit RulesBy default, when an attribute being validated is not present or contains an empty string, normal validation rules, including custom rules, are not run. For example, the rule will not be run against an empty string:
37 For a custom rule to run even when an attribute is empty, the rule must imply that the attribute is required. To quickly generate a new implicit rule object, you may use the
74 Artisan command with the
85 option: |