0

my client side validation is not working after creating scenarios. it works fine before creating scenario . i have two scenarios update and create, i've two field to be required on create scenario and i've one field to be required on update scenario my model

        class BaseBroadcast extends BaseActiveRecord
    {
      const SCENARIO_UPDATE = 'update';
      const SCENARIO_CREATE = 'create';


/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [
            [
                'freight_charges_unit',
                'requested_response_date',
                'close_reason'
            ],
            'required'
        ],
       ......
    ];
}

/**
 * @return array
 */
public function scenarios(): array
{
    parent::scenarios();
    return [
        self::SCENARIO_UPDATE => ['close_reason'],
        self::SCENARIO_CREATE => ['freight_charges_unit','requested_response_date']

    ];
}

}

and i used create scenario like

$model = new Broadcast();
$model->scenario = Broadcast::SCENARIO_CREATE;

this and update scenario like

$model = Broadcast::findOne(['id' => $id]);
$model->scenario = Broadcast::SCENARIO_UPDATE;

this.

i'm enabled client side validation on my forms

 <?php $form = ActiveForm::begin(['action' => ......,
    'enableClientValidation' => true,]) ?>
Ajith Lal
  • 83
  • 2
  • 9
  • So what is happening? These fields are not validated as required anymore with scenarios? – Bizley Apr 26 '17 at 06:27
  • yes. when i click on submit with required field as empty the form is submitting – Ajith Lal Apr 26 '17 at 06:31
  • You don't have to set `'enableClientValidation' => true` - this is default and this line `parent::scenarios();` does nothing. Anyway could you show the code of controller's action that operates on this model? – Bizley Apr 26 '17 at 06:37
  • **update scenario action** `public function actionClose($id) { $model = Broadcast::findOne(['id' => $id]); $model->status = Broadcast::STATUS_CLOSED; $model->scenario = Broadcast::SCENARIO_UPDATE; if ($model->load(post()) && $model->update(true, ['close_reason', 'status'])) { \setFlash('success', Yii::t('company', 'Broadcast Closed Successfully!!!')); return $this->redirect(Yii::$app->request->referrer); } return $this->render('_closeReasonForm', ['model' => $model]); }` – Ajith Lal Apr 26 '17 at 06:40
  • Not sure what `post()` is but it looks ok. Can you show `_closeReasonForm` as well? Update question with it, it's hard to read code without formatting. – Bizley Apr 26 '17 at 06:47
  • try to call rules function after scenarion function call, and use , 'on' => 'update' after your validation rule – Vishva G Apr 26 '17 at 06:53

1 Answers1

0

Try this order on model

const SCENARIO_UPDATE = 'update';
const SCENARIO_CREATE = 'create';


public function scenarios()
{
    return [
        self::SCENARIO_UPDATE => ['close_reason'],
        self::SCENARIO_CREATE => ['freight_charges_unit','requested_response_date']
    ];
}

public function rules()
{
  return [
            [['freight_charges_unit','requested_response_date'], 'required','on' => 'create'],
            [['close_reason'], 'required','on' => 'update'],
    ];
}

And this in controller

$model = new Broadcast(['scenario' => 'create']);

//for update

$model = $this->findModel($id);

$model->scenario = 'update';

Vishva G
  • 399
  • 7
  • 25