-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
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 $year, string $month, string $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($str, string $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; } }
-
demyr
Senior Member
-
Posts: 312
Threads: 17
Joined: Sep 2018
Reputation:
6
Hi.
Why do you work with string but not integer ?
And why you don't easily work with input type date . Because, you can easily explode the date you receive in your controller and go ??
-
InstantT
Junior Member
-
Posts: 25
Threads: 8
Joined: Jun 2022
Reputation:
2
-
AgBRAT
Junior Member
-
Posts: 25
Threads: 11
Joined: May 2022
Reputation:
0
06-29-2022, 08:34 AM
(This post was last modified: 06-29-2022, 08:42 AM by AgBRAT.)
I solved it thank you for the help!
This is what I ended up doing with my custom rule:
PHP Code: public function validateDate($str, string $fields, array $data) { if (checkdate((int) $data['bday_month'], (int) $data['bday_day'], (int) $data['bday_year'])) { return TRUE; } else {
return FALSE; }
}
And 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');
$validation->getRuleGroup('date'); $validation->setRuleGroup('date'); $validation->run(['bday_month' => $bday_month, 'bday_day' => $bday_day, 'bday_year' => $bday_year], 'date');
(06-29-2022, 08:34 AM)AgBRAT Wrote: I solved it thank you for the help!
This is what I ended up doing with my custom rule:
PHP Code: public function validateDate($str, string $fields, array $data) { if (checkdate((int) $data['bday_month'], (int) $data['bday_day'], (int) $data['bday_year'])) { return TRUE; } else {
return FALSE; }
}
And 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');
$validation->getRuleGroup('date'); $validation->setRuleGroup('date'); $validation->run(['bday_month' => $bday_month, 'bday_day' => $bday_day, 'bday_year' => $bday_year], 'date');
I also format the date time for use in my views with an entity:
PHP Code: public function setAge(string $profile) { $db = \Config\Database::connect(); $this->builder = $db->table('users');
$user = $this->userModel->getUser($profile);
$month = $user->bday_month; $day = $user->bday_day; $year = $user->bday_year;
$bday = Time::create($year, $month, $day);
$this->attributes['age'] = $bday->toDateTimeString();
}
public function getAge(string $format = 'words') { if ($format === 'words' && $this->attributes['age'] !== NULL) {
return $this->attributes['age'] = get_date_words($this->attributes['age']); } elseif ($format === 'none' && $this->attributes['birthday'] !== NULL) {
return $this->attributes['age']; } }
|