Welcome Guest, Not a member yet? Register   Sign In
Custom Validate Birthday with value for each $year, $month, $day
#1

I am trying to use a custom rule to validate user's birthday (ie: so there's no February 31st). In my form I have the values separated by month, day, and year. Currently I take the post data for these values and convert them to a formatted string before sending to my custom rule (yyyy/mm/dd). And then I use checkdate() function to validate the date. But I keep getting a validation error that the date is invalid no matter what date I input.

Here is my Controller: 
PHP Code:
$bday_year $this->request->getPost('bday_year');
            $bday_month $this->request->getPost('bday_month');
            $bday_day $this->request->getPost('bday_day');

            $bday $this->userModel->formatDateString($bday_year$bday_month$bday_year);

            $validation->getRuleGroup('date');
            $validation->setRuleGroup('date');
            $validation->run(['date' => $bday], 'date'); 

Here is my Model: 
PHP Code:
public function formatDateString(string $yearstring $monthstring $day)
    {
        if (! empty($year) && ! empty($month) && ! empty($day)) {

            $date $year '/' $month '/' $day;
        
            
return $date;
        
        
} else {
            
            
return NULL;
        }
    

Here is my Custom Rule:
PHP Code:
public function validateDate($strstring $fields, array $data)
    {
        if ($data['date'] !==  NULL && is_string($data['date'])) {

            $tempDate explode('/'$data['date']);

            $year = (int) $tempDate[0];
            $month = (int) $tempDate[1];
            $day = (int) $tempDate[2];

            if (is_int($year) && is_int($month) && is_int($day)) {
        
                
if (checkdate($month$day$year)) {
                
                    
return TRUE;
            
                
} else {

                    return FALSE;
                }
            }
        } else {

            return FALSE;
        }
    
Reply


Messages In This Thread
Custom Validate Birthday with value for each $year, $month, $day - by AgBRAT - 06-28-2022, 02:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB