yii2yii2-advanced-appyii2-modelyii2-validation

redirect to previous address "from which form data was submitted" with model in yii2


I have created a widget to render the form in layouts/main.php footer section.

This is a widget file named common\widget\SubscriberFormWidget.php

<?php
namespace common\widgets;

use Yii;
use yii\base\Widget;
use common\models\Subscriber;

class SubscriberFormWidget extends Widget
{

    /**
      *@return string
      */

    public function run()
    {
        $model = new Subscriber();
         return $this->render('_form', [
            'model' => $model
        ]);
    }
}

This is _form file used in above widget located in common\widget\views\_form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Subscriber */
/* @var $form yii\widgets\ActiveForm */
?>

    <?php $form = ActiveForm::begin(['action' => ['subscriber/subscribe']]); ?>
    <div class="row">
        <div class="col-md-6">
            <?= $form->field($model, 'email')->textInput(['maxlength' => true, 'placeholder' => 'Enter @ Email to subscribe!'])->label(false) ?>
        </div>
        <div class="col-md-6">
            <?= Html::submitButton('Subscribe', ['class' => 'btn btn-success']) ?>
        </div>
    </div>

    <?php ActiveForm::end(); ?>

This is controller file frontend\controllers\SubscriberController.php

<?php

namespace frontend\controllers;

use Yii;
use common\models\Subscriber;
use common\models\SubscriberSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * SubscriberController implements the CRUD actions for Subscriber model.
 */
class SubscriberController extends Controller
{


    /**
     * Creates a new Subscriber model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionSubscribe()
    {
        $model = new Subscriber();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail()) {
                Yii::$app->session->setFlash('success', 'You have successfully subscribed My-Blog. You will get notification whenever New post is published');

                return $this->redirect(Yii::$app->request->referrer);
            } else {
                Yii::$app->session->setFlash('error', 'Sorry, we are unable to subscribe for the provided email address.');
            }
        }

        return $this->redirect([Yii::$app->request->referrer, ['model' => $model]]);
    }

    /**
     * Creates a new Subscriber model.
     * If unsubscribe is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
}

I have used widget in layouts\main.php in footer section

<footer class="footer">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <p class="pull-left">&copy; <?= Html::encode(Yii::$app->name) ?> <?= date('Y') ?></p>
            </div>
            <div class="col-md-6 text-justify" style="border-left : 2px solid black; border-right: 2px solid black">
                <?= SubscriberFormWidget::widget(); ?>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into elec
                </p>
            </div>
            <div class="col-md-3">
                 <p class="pull-right"><?= Yii::powered() ?></p>
            </div>
        </div>
    </div>
</footer>

This is model used for controller common\models\Subscriber.php

   <?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Expression;

/**
 * This is the model class for table "subscriber".
 *
 * @property int $id
 * @property string $email
 * @property string $token
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class Subscriber extends \yii\db\ActiveRecord
{
    const STATUS_DEACTIVE = 0;
    const STATUS_ACTIVE = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'subscriber';
    }

    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                'value' => new Expression('NOW()'),
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['email'], 'required'],
            [['status', 'created_at', 'updated_at'], 'safe'],
            [['email'], 'string', 'max' => 60],
            [['token'], 'string', 'max' => 255],
            [['token'], 'unique'],
            [['email'], 'unique',  'targetClass' => '\common\models\Subscriber', 'message' => 'This email has already subscribed our blog.','filter' => ['!=','status' ,0]],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'email' => 'Email',
            'token' => 'Token',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * Generates subscriber token
     */
    public function generateSubscriberToken()
    {
       return $this->token = Yii::$app->security->generateRandomString() . '_' . time();
    }

    /**
     * Send Email when successfully subscribe
     */

     public function sendEmail()
    {
        $subscribers = self::find()->where(['email' => $this->email])->one();

        //set flag for sending email
        $sendMail = false;
        //email subject
        $subject = '';

        //generate token
        $token = $this->generateSubscriberToken();

        //if email found in subscribers
        if ($subscribers !== null) {

            //check if inactive
            if ($subscribers->status !== self::STATUS_ACTIVE) {

                //assign token
                $subscribers->token = $token;

                //set status to active
                $subscribers->status = self::STATUS_ACTIVE;

                print_r($subscribers->errors);
                //update the recrod
                if (!$subscribers->save()) {
                    return false;
                }

                //set subject
                $subject = 'Welcome back ' . $this->email . 'Thank you for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
                $sendMail = true;
            }
        } else { //if email does not exist only then insert a new record
            $this->status = 1;
            if (!$this->save()) {

                return false;
            }
            $subject = 'Thank you ' . $this->email . ' for subscribing ' . Yii::$app->name . '<br /><br /> You will receive notification whenever new trick or post is published to website';
            $sendMail = true;
        }

        //check if send mail flag set
        if ($sendMail) {
            return Yii::$app->mailer
                ->compose()
                ->setFrom(['noreply@my-blog.com' => Yii::$app->name . ' robot'])
                ->setTo('piyush@localhost')
                ->setSubject('Subscription : ' . Yii::$app->name)
                ->setHtmlBody($subject)
                ->send();
        }

    }
}

Now I want form to be worked with validation. If user enter any wrong input like already registered then, message for this should return to view file from which submitted form data.


Solution

  • Controlelr::redirect() accepts a url and an optional status code parameter.
    You're not calling it correctly in the snippet you posed.
    I believe you're trying to only use the url parameter (a single array argument) and skip the status code.

    You also cannot rely on referrer to point you back to previous page. You need to save the route to return to in the form page

    Url::remember([Yii::$app->requestedRoute]);
    

    and later use that to return back to the form

    return $this->redirect([Url::previous(), ['model' => $model]]);