What are the new different validation rules in Laravel?

Laravel provides many different approaches to validate an application’s incoming data. The most common one is to use the validate function that is available on all incoming HTTP requests. However, we have many other approaches to validate incoming data. In this tutorial, let us go through different validation rules in Laravel.

Laravel has a wide variety of convenient validation rules that we can apply to data. This also provides the ability to validate values that should be unique in a given database table. You can add Validation rules in the Controller class in the function where we save user data.

List of different Validation Rules

As per Laravel 9, you can use the rules below inside the validate function.

Commonly used rules

  • Alpha: to validate only alphabets.
  • Alpha Dash: to validate alpha-numeric characters, dashes, and underscores. Useful to validate password input.
  • Array: to validate if the input is a PHP array.
  • Between: to validate if the entered value lies between two min and max (inclusive) values.
  • Boolean: to assure the entered value can be cast as a Boolean.
  • Date: to assure if the input is a Date as per the strtotime PHP function.
  • Date Format: the input should match a given data format. Useful for forms where a specific date format should be entered to assure similar entries for date formats.
  • Different: to assure distinct values for two fields. The input must have a different value compared to some other field.
  • Email: to validate if the input is of the email format.
  • Exists: the field should be present in a database table. For example, state => ‘exists:states’. State should be a column of the states table.
  • Image: to validate if the file uploaded is an image. Checks against image formats (jpg, png, jpeg, bmp, gif, svg, or webp)
  • Integer: to assure for integer values in inputs.
  • MIME Types (File): to check if the uploaded (video) file supports a set of mime types. For example, as shown below.
'video' => 'mimetypes:video/m3u8/mpeg,video/quicktime'
  • Regular Expression: the regex pattern function to check any given string pattern. We can use it for email, lat long validation, and many other cases where we need to match string patterns for the input. It works like the preg_match function in PHP.
  • Required: to assure there are not any empty or null values
'name' => 'size:12';
  • String: The input must be a string. To allow null entries, assign the nullable rule to the field.
  • Unique (Database): to assure unique column records for a database table.
  • URL: to assure that the input is a valid URL.

Rarely used Rules

  • Accepted: The field under validation must have a Boolean status. Useful to validate “Terms of Service” acceptance or similar fields.
  • Active URL: The hostname of a URL is extracted using the PHP function of parse_url and is passed to the dns_get_record function to check if the entered URL is valid.
  • After (Date): To validate the value after a given date, Dates are parsed into the PHP function strtotime to be converted to a DateTime instance.
  • Before (Date): The value preceding a given date. The dates are passed into the PHP strtotime function so they should be converted into a valid DateTime instance.
  • Confirmed: the input field must match another field. For example, the confirm password field should match the password field like {password}_confirmation.
  • In: as we use the ‘in’ in SQL. This rule checks if the input field is one of the values entered in the in function.
  • IP Address: to assure that the input is an IP address.
  • JSON: to validate a JSON string.
  • Not In: as we use the ‘in’ in SQL. This rule checks if the input field is not one of the values present in the not-in function.
  • Required If: an exception of the required rule, applies the ‘required’ rule for certain cases. For example if another field has a specific value or something.
  • Required Unless: another exception for the ‘required’ rule. To. make the rule apply unless another value is present or equals something. For example, we can use it to validate the confirm password field, as it. should have the ‘required’ rule only if the password is entered.
  • Same: the input should be ‘same’. or match another field. For example ‘same(titles)’.
  • Timezone: The input must be a valid timezone identifier as per the timezone_identifiers_list PHP function.

Rules for Integers

  • Digits: to validate the number of digits for an input field.
  • Digits Between: to confirm if the input has digits between specific values. For example, digits_between: 6,10 is to check if the input has the digits 7, 8, 9.
  • Numeric: to assure that the input has all numeric values.
  • Max: to assure that the entered value is less than a maximum number.
  • Min: to assure that the entered value is greater than a minimum number.

Leave a Comment