How to create text input without model?

Hi,

How do we add text input fields to a form without a model and how to catch/get them on the backend?

I would like for the text input fields to retain the css.

I want the user to be able to choose a new password and need them to type it twice. I will then validate it and update the relevant field in the DB.

So I need something like this:




<?= $form->field('password1')->textInput(['maxlength' => 10, 'style' => 'width:350px'])->label('New Password') ?>

<?= $form->field('password2')->textInput(['maxlength' => 10, 'style' => 'width:350px'])->label('Repeat New Password') ?>



I will then validate if password1 is exactly the same as password2 and then update the correct property on the model.

Okay, I got it to this:




<?= Html::input('text','password1','', $options=['class'=>'form-control','maxlength'=>10, 'style'=>'width:350px']) ?>



Now just not sure how to ad a label and hint?

I changed to this and it looks perfectly fine:




<b>New Password</b><?= Html::input('text','password1','', $options=['class'=>'form-control', 'maxlength'=>10, 'style'=>'width:350px']) ?>Passwords may only use characters A-Z a-z 0-9



The normal way to do this is to use a ViewModel, that is a model that inherits yii\base\Model rather than yii\db\ActiveRecord. In the ViewModel, you have a property for both passwords and then inside that model, on save, you check that they match and save them into the User model, which is linked to the database.

You can have validation in the ViewModel (using normal validators like ‘match’ validator) and you can also set property labels so this is a neater and more correct way tp achieve this.

There are examples of these types on models in the advanced template (and the ContactForm is another basic example).

1 Like

Another simple way - perhaps not the Yii way, but I don’t see any issues with doing it this way is to simply include native HTML code.

    <div class="form-group">
        <label for="somefield">Some Description</label>
        <textarea class="form-control w-75" id="somefield" rows="3"></textarea>
    </div>