In-Depth Guide to Laravel Validation Rules for Special Fields
Laravel provides a powerful and flexible validation system that lets developers ensure data accuracy, completeness, and security with ease. This guide focuses on core validation rules, field-specific validations, and regex-based customizations for various input types, covering usernames, emails, passwords, phone numbers, dates, and more. We'll also include conditional and specialized rules for precise validation control.
1. Basic Validation Rules for Common Fields
These core validation rules set the foundation for input requirements, covering presence, nullability, and data type.
required: Field must be present and non-empty.nullable: Allows a field to be empty ornull.present: Ensures the field is in the request, even if it’s empty.filled: Field must have a value if present.sometimes: Applies validation only if the field is included.
Example:
'username' => 'required|string|max:50',
'age' => 'nullable|integer|min:0',
2. String and Length-Specific Rules
String fields often require specific lengths or alphabetic/alpha-numeric characters.
string: Checks if the value is a string.min:<number>: Minimum character length.max:<number>: Maximum character length.between:<min>,<max>: Value length within a range.size:<number>: Exact length.
Example:
'username' => 'required|string|between:5,15|alpha_num|unique:users,username',
3. Regex-Based Custom Validation for Special Fields
Regex enables enforcing patterns like alphanumeric usernames or complex password formats.
regex:<pattern>: Validates with a custom regular expression.
Common Patterns:
- Username: Only alphanumeric, 5-15 characters.
- Password: Minimum 8 characters, at least one letter and one number.
- Phone Number: Allows a Pakistani format (e.g., +92 or 0 followed by 10 digits).
- Address: Allows letters, numbers, and symbols such as spaces, commas, periods, or hyphens.
Example:
'username' => 'required|string|between:5,15|alpha_num|unique:users,username',
'password' => 'required|string|min:8|confirmed|regex:/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/',
'phone' => 'nullable|numeric|digits_between:10,15|regex:/^(\+92|0)?[0-9]{10}$/',
'address' => 'nullable|string|max:255|regex:/^[\w\s,.-]+$/',
4. Date-Specific Validation Rules
Date rules can enforce valid date formats, ranges, and conditions for relative comparisons.
date: Checks if the field is a valid date.before:<date>: Date must be before a specific date.after:<date>: Date must be after a specific date.date_equals:<date>: Exact date match.date_format:<format>: Ensures the date follows a specified format.
Example:
public function rules()
{
return [
'username' => 'required|string|between:5,15|alpha_num|unique:users,username',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8|confirmed|regex:/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/',
'phone' => 'nullable|numeric|digits_between:10,15|regex:/^(\+92|0)?[0-9]{10}$/',
'date_of_birth' => 'required|date|before:today',
'profile_picture' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'address' => 'required_with:city,zipcode|string|max:255|regex:/^[\w\s,.-]+$/',
'status' => 'required_if:role,admin|in:active,inactive',
'salary' => 'nullable|numeric|between:1000,50000|decimal:2',
'json_field' => 'nullable|json',
'roles' => 'array|distinct',
'roles.*' => 'exists:roles,id',
];
}
Conclusion
With Laravel’s extensive validation options, you can tailor your application’s validation to meet almost any requirement. From simple presence checks to complex conditional and regex-based validations, Laravel makes it easy to ensure that your data is clean and compliant with business rules. Following these examples will give you a strong foundation in crafting validations for any type of data, whether it’s numeric, text, file uploads, or arrays.
BryanObsex
Ally us today at 1xBet benefit of the fundamental online cricket betting sample! We offer the outdo and most tempting cricket odds recompense prodigious winnings.
Don’t pass up loophole on the hazard to win gigantic with 1xBet!
Thumb our sweeping cricket betting lines and fancy a unique and exciting participation like no other.
[url=https://refpa7921972.top/L?tag=s_3464116m_355c_2422859&site=3464116&ad=355&r=en/line/cricket]1xbet[/url]