File: /home/jubaroyal/public_html/app/Rules/AgeBetween.php
<?php
namespace App\Rules;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;
class AgeBetween implements Rule
{
private $minAge;
private $maxAge;
/**
* Create a new rule instance.
*
* @param int $minAge
* @param int $maxAge
* @return void
*/
public function __construct(int $minAge, int $maxAge)
{
$this->minAge = $minAge;
$this->maxAge = $maxAge;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$today = Carbon::today();
$dob = Carbon::parse($value);
$age = $dob->diffInYears($today);
return $age >= $this->minAge && $age <= $this->maxAge;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must give age between ' . $this->minAge . ' and ' . $this->maxAge . ' years old.';
}
}