禁用数据库错误

Disable error from database

我有一张注册表,我在其中进行验证,但无法正常工作。用户选择他们的出生日期,但是,如果他们选择一个不存在的日期,例如。 2016-02-31 将发生 SQL 错误:

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', 'ania@wp.pl', 1, 2, 'Ania', NULL, 'Kowalska', 'yN.16FSerWx6IEbT1OZjBOReMeiWslCj..fhqDvgePxqg3jZhdanG', 'mMZ-MyAGRSdzH_VhN0qT5UEKPjiPvw3h', 10, 1453830416, 1453830416)

当我点击后退按钮(向左箭头)时出现此错误:

Yii::$app->getSession()->setFlash('error', 'Incorrect date');

我该怎么做才能消除第一个错误并强制页面保留用于显示我的消息的注册表单。我尝试实施 try/catch 但我不知道这样做是否合适。

我用这个函数返回数据:

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');
      
}

我在函数 singnup 中调用了我的 getDbFormatedDate() 所以很难在那里使用 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;
    }

更新:

 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;
}

好的,你不需要 if,像这样:

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;
}

然后,如果您调用 getDbFormattedDate(),请执行以下操作:

if (!is_null($this->getDbFormatedDate())
    // Handle your database insert