Yii 2 Scenarios

Create scenarios in model:

class Customer extends \yii\db\ActiveRecord
{
    // Scenarios
    const SCENARIO_NOTES_UPDATE = 'notes_update';
 
    //...
 
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            //...
 
            // notes is required in "notes_update" scenario
            [['id', 'notes'], 'required', 'on' => self::SCENARIO_NOTES_UPDATE],  // list all fields required in scenario
        ];
    }
 
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_NOTES_UPDATE] = ['notes'];  // required fields for this scenario
        return $scenarios;
    }
 
}    

Create a model using a specific scenario:

class CustomerController extends Controller
{
    //...
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => [
                            //...
                            'update-notes',
                        ],
                        'allow' => true,
                        'roles' => ['@'],  // @ = Authenticated users
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
 
    public function actionUpdateNotes($id)
    {
        $model = $this->findModel($id);
        $model->scenario = Customer::SCENARIO_NOTES_UPDATE;
 
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id, 'tab'=>'notes']);
        } else {
            return $this->render('update-notes', [
                'model' => $model,
            ]);
        }
    }
}    

Create a view that uses the scenario:

<?php
 
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\bootstrap\Tabs;
 
/* @var $this yii\web\View */
/* @var $model app\models\Customer */
/* @var $form yii\widgets\ActiveForm */
 
$this->title = Yii::t('app', 'Update Customer Notes') . ': ' . $model->account_number. ' ' . $model->company_name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Customers'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->account_number, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('app', 'Update Notes');
?>
<div class="customer-update">
 
    <h1><?= Html::encode($this->title) ?></h1>
 
    <div class="customer-form">
 
        <?php $form = ActiveForm::begin(); ?>
 
        <div class="row">
        <div class="col-md-12"><br />
        <?= $form->field($model, 'notes')->textarea(['rows' => 20]) ?>
        </div>
        </div>
 
        <div class="col-md-12">
            <div class="form-group">
                <?= Html::submitButton($model->isNewRecord ? 
                    Yii::t('app', 'Create') : 
                    Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary'])
                ?>
                <?= (!$model->isNewRecord ? Html::a(Yii::t('app', 'Cancel'), ['view', 'id' => $model->id, 'tab'=>'notes'], ['class' => 'btn btn-default']) : '') ?> 
            </div>
        </div>
 
 
        <?php ActiveForm::end(); ?>
 
    </div>
</div>
References