Different Scenarios Of Client Validation.

Hello,

For example, we have a form and a two buttons. The first button simply saves content and we check only constraints for fields like a sting length etc.

The second button publishes content and all fields are required.

Is there way to choose between several scenarios of client validation before the form will be submitted?

Thank you.

Something like this…

in the view:




//First button

<?php $form = ActiveForm::begin(['action' => ['/simplysave']]); ?>

<?= Html::submitButton('Simply save')?> 

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


//Second button

<?php $form = ActiveForm::begin(['action' => ['/publish']]); ?>

<?= Html::submitButton('Publish')?> 

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



in the control:




public function actionSimplySave()

{

    $model = new Model([scenario => 'simplysave']);

    if ($model->load(Yii::$app->request->post()) {

        $model->validate();

        //whatever...

    }

}


// ...


public function actionPublish()

{

    $model = new Model([scenario => 'publish']);

    if ($model->load(Yii::$app->request->post()) {

        $model->validate();

        //whatever...

    }

}



Thank you for reply, but I mean client-side validation that is implemented by JavaScript.

As I understand it, the form renders an implementation of rules set for the current scenario and there is no way to change it on the browser side.

stuff.cebe.cc/yii2docs/guide-input-validation.html check out the conditional validation section. See the "when" and "whenClient" options.

Thank you very much, it helped me.