0
votes

I want to use the simple form tag & element like

<form name="formname" action="" method="post">
<input type="text" name="title" value="" />
</form>

I want to validate all fields on client side & server side using yii model. Model validations can easily apply with activeform but i don't want to use activeform.

Any easy way to validate the form fields on client & server both sides?

1
Why do you not want to use ActiveForm? - marche
it's always better to use active form, but you can use normal HTML from. And use Model for server side for validation. - Harish Kurup

1 Answers

0
votes

Use beginForm() method. And try something like below.

use yii\helpers\Html;
<?php $form = Html::beginForm()([
    'method' => 'post',
    'name' => 'formname',
]); ?>
<?= Html::textarea->textarea(['rows' => 6, 'name'=>'title'])->label(false) ?>
<div class="form-group">
    <?= Html::submitButton('POST', ['class' => 'btn btn-primary']) ?>
</div>
<?php Html::endForm() ?>

and then in your model

if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    // $model->addRule(['fieldname'], 'string', ['max' => 50]);
}