Validations In Laravel
One of the most important things while developing an application is validation. They play a vital role by throwing a 422 status error to the client side if the data is not as we need it.
In this blog, we will be diving deep into the necessary validation that is required to stop the unwanted data feed into our database.
1. Basic Validation
- Required: The field must be present and not empty
'name' => 'required'
- Min/Max: Sets minimum and maximum values for numeric fields, string lengths, or array elements
'age' => 'min:18|max:60',
'title' => 'min:5|max:100'
- Email: Ensures the field is a valid email address.
'email' => 'required|email'
- In/Not In: Validates that a value is in (or not in) a given set of values
'status' => 'in:active,inactive',
'category' => 'not_in:forbidden,restricted'
- Nullable: This shows that the value can be null with some validations.
'email' => 'nullable|email'
- Boolean: Checks if a field is either true or false.
'active' => 'boolean'
2. String Validation
- String: Ensures the value is a string.
'description' => 'string'
- Alpha: Only allows alphabetic characters (no spaces).
'first_name' => 'alpha'
- Alpha Dash: Allows alphabetic characters, dashes, and underscores
'username' => 'alpha_dash'
- Alpha Numeric: Only allows alphabetic and numeric characters
'code' => 'alpha_num'
3. Number Validation
- Integer: Ensures the field is an integer
'age' => 'integer'
- Numeric: Allows any numeric value, including floats
'price' => 'numeric'
- Digits/Digits Between: Ensures a numeric field has a specified number of digits
'pincode' => 'digits:6',
'phone' => 'digits_between:10,15'
4. Date and Time Validation
- Date: Validates that the field is a date.
'dob' => 'date'
- Date Format: Validates that a date matches a specific format.
'start_date' => 'date_format:Y-m-d'
- Before/After: Ensures the date is before or after a specified date
'start_date' => 'before:end_date',
'end_date' => 'after:today'
5. File Validation
- File: Validates that the key contains a file
'profile_image' => 'file'
- Mimes/Mimetypes: Restricts file types to specific MIME types or extensions
'profile_image' => 'mimes:jpg,png,jpeg',
'document' => 'mimetypes:application/pdf'
- Size: Specifies the file size in kilobytes (KB)
'profile_image' => 'size:2048'
6. Conditional Validation
- Validation based on the values of other fields
'email' => 'nullable|email',
'phone' => 'required_without:email'
The Above block of code indicates that if the email is given by the user then the phone is not required and if the phone is given then the email is not required.
This fulfills the criteria that either phone or email is required
7. Array Validation
- Array: Allows only array
'experience' => 'required|array',
'address' => 'nullable|array',
- All the array values must be string
'address.*' => 'string'
- Validating the particular keys
'experience.*.company_name' => 'required|string',
'experience.*.position' => 'nullable|string',
'experience.*.year' => 'required|numeric|min:1',
8. Additional Validations
- Unique: Ensures the value is unique in a specified database table
'email' => 'unique:users'
or
'email' => 'unique:users,email'
- Exists: Checks if the value exists in a specified table/column.
'user_id' => 'exists:users,id'
- Confirmed: Ensures that a field has a matching
{field}_confirmation
field (typically used for passwords or account numbers).
'password' => 'confirmed',
'account_number' => 'confirmed',
The code checks that the "password" and "password_confirmation" keys are the same, as well as the "account_number" and "account_number_confirmation" keys.
Custom Validation Message
Laravel has predefined messages for all the validations but if you want a custom message according to your application you can customize it.
$request->validate([
'email' => 'required|unique:users,email',
'password' => 'confirmed',
'profile_image' => 'required|size:2048'
],
[
'email.required' => 'Email Field is mandatory',
'email.unique' => 'Email already exists! please input different email address',
'profile_image.size' => 'Profile Image should not be greater than 2 MB',
]);
This is how we can create a custom message for the validation we write.
It is not mandatory to write a message for all the rules; only write for the rules that require validation customization messages.
This is it; it all revolves around the essential validations that are primarily required.
Feel free to comment if you have any recommendations.
Keep coding and exploring ❤️