Commit 80dde071 authored by Shakarim Sapa's avatar Shakarim Sapa

- Add new validation rule

parent c1ee8886
...@@ -81,6 +81,8 @@ class Bid extends \common\components\ActiveRecordModel ...@@ -81,6 +81,8 @@ class Bid extends \common\components\ActiveRecordModel
[['email'], 'required', 'on' => self::SCENARIO_SUBSCRIBE], [['email'], 'required', 'on' => self::SCENARIO_SUBSCRIBE],
[['email'], 'checkUnique', 'on' => self::SCENARIO_SUBSCRIBE, 'message' => 'Вы уже подписаны на новости блога'],
[['text'], 'string'], [['text'], 'string'],
[['name'], 'string', 'max' => 100], [['name'], 'string', 'max' => 100],
...@@ -91,13 +93,10 @@ class Bid extends \common\components\ActiveRecordModel ...@@ -91,13 +93,10 @@ class Bid extends \common\components\ActiveRecordModel
]; ];
} }
public function beforeSave($insert){ public function checkUnique($attribute, $params){
if (!parent::beforeSave($insert)) if (!is_null($this->$attribute)) {
return false; if (Bid::find()->where([$attribute => $this->$attribute])->exists()===true) {
$this->addError($attribute, $params['message']);
if (!is_null($this->email)) {
if (Bid::find()->where(['email' => $this->email])->exists()===true) {
$this->addError('email', 'Вы уже подписаны на новости блога');
return false; return false;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment