Kanika
Kanika

Reputation: 61

Add success message just like add error Yii2

I'm trying to show success message just like $this->addError($attribute, 'Invalid Promo Code'); this code but unable to do this is there any way to achieve this functionality: My model code :

  public function rules()
    {
         return [
            ['referralCode', 'validateReferralCode'],
    ];
    }

And this is function for validation :

public function validatePromoCode($attribute, $params){

    if ($this->$attribute != '') {
        $model = PromoCode::find()
                    ->where(['promo_code'=>$this->promoCode,'status'=>1])
                    ->andWhere('end_date<='.time())->one();
        if(!$this->hasErrors() && ($model))
        {  
            // want to add success message here 
            $this->addSuccess($attribute, 'You will get 20 points');
            return true;
        }else{
             $this->addError($attribute, 'Invalid Promo Code');
        }
    }

    return $this->referralCode;
}

Upvotes: 0

Views: 254

Answers (1)

Yupik
Yupik

Reputation: 5032

As the Yii2 Model Docs says - there's no method like addSuccess(). Only way to do this is to write your own functionality to handle this methods, and proper javascript to handle inputs.

Upvotes: 2

Related Questions