How to install and Use CKEditor in Laravel 10
Read More
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.
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 or null
.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',
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',
Regex enables enforcing patterns like alphanumeric usernames or complex password formats.
regex:<pattern>
: Validates with a custom regular expression.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,.-]+$/',
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',
];
}
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.
Recent posts form our Blog
0 Comments
Like 1