kav
(Mail)
August 22, 2014, 8:45am
1
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...
}
}
kav
(Mail)
August 22, 2014, 2:42pm
3
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.
martingeorg
(Martin Georgiev12345)
August 25, 2014, 5:43am
4
stuff.cebe.cc/yii2docs/guide-input-validation.html check out the conditional validation section. See the "when" and "whenClient" options.
kav
(Mail)
August 25, 2014, 6:57am
5
Thank you very much, it helped me.