List The Available Validation Rules In Laravel With Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Laravel provides several different approaches to validate your application's incoming data. By default, Laravel's base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.

List The Available Validation Rules In Laravel With Example

Below is a list of all available validation rules and their function:
 

Accepted
Active URL
After (Date)
After Or Equal (Date)
Alpha
Alpha Dash
Alpha Numeric
Array
Bail
Before (Date)
Before Or Equal (Date)
Between
Boolean
Confirmed
Date
Date Equals
Date Format
Different
Digits
Digits Between
Dimensions (Image Files)
Distinct
Email

Ends With
Exclude If
Exclude Unless
Exists (Database)
File
Filled
Greater Than
Greater Than Or Equal
Image (File)
In
In Array
Integer
IP Address
JSON
Less Than
Less Than Or Equal
Max
MIME Types
MIME Type By File Extension
Min
Not In
Not Regex

Nullable
Numeric
Password
Present
Regular Expression
Required
Required If
Required Unless
Required With
Required With All
Required Without
Required Without All
Same
Size
Sometimes
Starts With
String
Timezone
Unique (Database)
URL
UUID

 

accepted

The field under validation must be yeson1, or true. This is useful for validating "Terms of Service" acceptance.

 

active_url

The field under validation must have a valid A or AAAA record according to the dns_get_record PHP function. The hostname of the provided URL is extracted using the parse_url PHP function before being passed to dns_get_record.

 

after:date

The field under validation must be a value after a given date. The dates will be passed into the strtotime PHP function:

'start_date' => 'required|date|after:tomorrow'


Instead of passing a date string to be evaluated by strtotime, you may specify another field to compare against the date:

'finish_date' => 'required|date|after:start_date'

 

after_or_equal:date

The field under validation must be a value after or equal to the given date. For more information, see the after rule.

 

alpha

The field under validation must be entirely alphabetic characters.

 

alpha_dash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

 

alpha_num

The field under validation must be entirely alpha-numeric characters.

 

array

The field under validation must be a PHP array.

 

bail

Stop running validation rules after the first validation failure.

 

before:date

The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime function. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

 

before_or_equal:date

The field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP strtotime function. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

 

between:min,max

The field under validation must have a size between the given min and max. Strings, numerics, arrays, and files are evaluated in the same fashion as the size rule.

 

boolean

The field under validation must be able to be cast as a boolean. Accepted input are truefalse10"1", and "0".

 

confirmed

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

 

date

The field under validation must be a valid, non-relative date according to the strtotime PHP function.

 

date_equals:date

The field under validation must be equal to the given date. The dates will be passed into the PHP strtotime function.

 

date_format:format

The field under validation must match the given format. You should use either date or date_format when validating a field, not both. This validation rule supports all formats supported by PHP's DateTime class.

 

different:field

The field under validation must have a different value than field.

 

digits:value

The field under validation must be numeric and must have an exact length of value.

 

digits_between:min,max

The field under validation must be numeric and must have a length between the given min and max.

 

dimensions

The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:
 

'avatar' => 'dimensions:min_width=100,min_height=200'


Available constraints are: min_widthmax_widthmin_heightmax_heightwidthheightratio.

ratio constraint should be represented as width divided by height. This can be specified either by a statement like 3/2 or a float like 1.5:

'avatar' => 'dimensions:ratio=3/2'


Since this rule requires several arguments, you may use the Rule::dimensions method to fluently construct the rule:

use Illuminate\Validation\Rule;

Validator::make($data, [
    'avatar' => [
        'required',
        Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2),
    ],
]);

distinct

When working with arrays, the field under validation must not have any duplicate values.

'foo.*.id' => 'distinct'

 

email

The field under validation must be formatted as an e-mail address. Under the hood, this validation rule makes use of the egulias/email-validator package for validating the email address. By default the RFCValidation validator is applied, but you can apply other validation styles as well:

'email' => 'email:rfc,dns'


The example above will apply the RFCValidation and DNSCheckValidation validations. Here's a full list of validation styles you can apply:

  • rfcRFCValidation

  • strictNoRFCWarningsValidation

  • dnsDNSCheckValidation

  • spoofSpoofCheckValidation

  • filterFilterEmailValidation

The filter validator, which uses PHP's filter_var function under the hood, ships with Laravel and is Laravel's pre-5.8 behavior. The dns and spoof validators require the PHP intl extension.

 

ends_with:foo,bar,...

The field under validation must end with one of the given values.

 

exclude_if:anotherfield,value

The field under validation will be excluded from the request data returned by the validate and validated methods if the anotherfield field is equal to value.

 

exclude_unless:anotherfield,value

The field under validation will be excluded from the request data returned by the validate and validated methods unless anotherfield's field is equal to value.

 

exists:table,column

The field under validation must exist on a given database table.

Basic Usage Of Exists Rule

'state' => 'exists:states'


If the column option is not specified, the field name will be used.

Specifying A Custom Column Name

'state' => 'exists:states,abbreviation'


Occasionally, you may need to specify a specific database connection to be used for the exists query. You can accomplish this by prepending the connection name to the table name using "dot" syntax:

'email' => 'exists:connection.staff,email'


Instead of specifying the table name directly, you may specify the Eloquent model which should be used to determine the table name:

'user_id' => 'exists:App\User,id'


If you would like to customize the query executed by the validation rule, you may use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit them:

 

use Illuminate\Validation\Rule;
Validator::make($data, [
    'email' => [
        'required',
        Rule::exists('staff')->where(function ($query) {
            $query->where('account_id', 1);
        }),
    ],
]);

 

file

The field under validation must be a successfully uploaded file.

 

filled

The field under validation must not be empty when it is present.

 

gt:field

The field under validation must be greater 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 size rule.

 

gte:field

The field under validation must be greater 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 size rule.

 

image

The file under validation must be an image (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 implode an array, the Rule::in method may be used to fluently construct the rule:

 

use Illuminate\Validation\Rule;
Validator::make($data, [
    'zones' => [
        'required',
        Rule::in(['first-zone', 'second-zone']),
    ],
]);

 

in_array:anotherfield.*

The field under validation must exist in anotherfield's values.

 

integer

The field under validation must be an integer.

Related Post