In Laravel 8 form request validation's rules, I have this birth date validation:
public function rules()
{
    return [
        'birth_date' => [
            'required',
            'before: date("Y-m-d")'
        ],
    ];
}
How do I validate it to be 18 years less than or equal to today's date?
Use Carbon (built in to Laravel):
public function rules() {
  return [
    'birth_date' => [
      'required',
      'date_format:Y-m-d',
      'before:' . Carbon::now()->subYears(18)->format('Y-m-d')
    ],
  ];
}
This would generate before:2003-07-30. Make sure to include use Carbon\Carbon; at the top of this file, or do Carbon\Carbon::now()->subYears(18)->format('Y-m-d');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With