Reputation: 205
I have a registration form, where I do validation which is not working like I want. The user chooses their birth date but, if they choose a date that does not exist, ex. 2016-02-31 an SQL error will occur:
Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'BirthDate' cannot be null The SQL being executed was: INSERT INTO `urUser` (`Login`, `Email`, `RulesAccept`, `Rel_Sex`, `Name`, `BirthDate`, `Surname`, `PasswordHash`, `auth_key`, `status`, `created_at`, `updated_at`) VALUES ('ania', '[email protected]', 1, 2, 'Ania', NULL, 'Kowalska', '$2y$13$3N.16FSerWx6IEbT1OZjBOReMeiWslCj..fhqDvgePxqg3jZhdanG', 'mMZ-MyAGRSdzH_VhN0qT5UEKPjiPvw3h', 10, 1453830416, 1453830416)
When I click back button (left arrow) I get this error:
Yii::$app->getSession()->setFlash('error', 'Incorrect date');
What can I do to disable this first error and force the page to stay for the registration form where I will have my messages displayed. I tried to implement try
/catch
but I don't know if that in good place to do it.
I use this function to reurn data:
public function getDate() {
return $this->year . '-' . $this->month . '-' . $this->day;
}
public function getDbFormatedDate() {
if (checkdate($this->month, $this->day, $this->year)){
$dateDeadline = date_create($this->getDate());
Yii::$app->session->setFlash('success', Yii::t('app', 'Udało się zarejestrować'));
return date_format($dateDeadline, 'Y-m-d');
}
Yii::$app->getSession()->setFlash('error', 'Nieprawidłowa data');
}
I call to my getDbFormatedDate() in function singnup so it so hard to use there if !is_null:
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->Login = $this->login;
$user->Email = $this->email;
$user->RulesAccept=1;
$user->Rel_Sex = $this->sex;
$user->Name = $this->name;
$user->BirthDate = $this->getDbFormatedDate();
$user->Surname = $this->surname;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return false;
}
Upvotes: 0
Views: 74
Reputation: 5806
Update:
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->BirthDate = $this->getDbFormatedDate();
if (is_null($user->BirthDate))
return false;
$user->Login = $this->login;
$user->Email = $this->email;
$user->RulesAccept=1;
$user->Rel_Sex = $this->sex;
$user->Name = $this->name;
$user->Surname = $this->surname;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
return $user;
}
}
return false;
}
Okay, you don't need an if, like so:
public function getDate() {
return $this->year . '-' . $this->month . '-' . $this->day;
}
public function getDbFormatedDate() {
if (checkdate($this->month, $this->day, $this->year)){
$dateDeadline = date_create($this->getDate());
Yii::$app->session->setFlash('success', Yii::t('app', 'Udało się zarejestrować'));
return date_format($dateDeadline, 'Y-m-d');
}
Yii::$app->getSession()->setFlash('error', 'Nieprawidłowa data');
return null;
}
And then if you call getDbFormattedDate(), do this:
if (!is_null($this->getDbFormatedDate())
// Handle your database insert
Upvotes: 1