B
B
Bahodir Ismatov2018-08-13 14:25:51
Yii
Bahodir Ismatov, 2018-08-13 14:25:51

how to change registration form in yii2?

Hi all!
I want to add an extra field to the registration form.
can't get value from new field.
Here is the form
5b7169c081dd2851752742.png
Here is the code (view):

spoiler
<h5 class="title">Edit Profile</h5>
                </div>
                <div class="card-body">
                    <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
                        <div class="row">
                            <div class="col-md-4 pl-1">
                                <div class="form-group">
                                    <label>Login</label>
                                    <?= $form->field($model, 'username')->textInput(['class'=>'form-control', 'placeholder'=>'Login', 'autofocus' => true])->label(false) ?>
                                </div>
                            </div>
                            <div class="col-md-4 pl-1">
                                <div class="form-group">
                                    <label>Parol)</label>
                                    <?= $form->field($model, 'password')->passwordInput(['class'=>'form-control', 'placeholder'=>'parol', 'autofocus' => true])->label(false) ?>
                                </div>
                            </div>
                            <div class="col-md-4 pl-1">
                                <div class="form-group">
                                    <label for="exampleInputEmail1">Email</label>
                                    <?= $form->field($model, 'email')->textInput(['class'=>'form-control', 'placeholder'=>'E-mail', 'autofocus' => true])->label(false) ?>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 pr-1">
                                <div class="form-group">
                                    <label>Familiyangiz</label>
                                    <?= $form->field($model, 'first_name')->textInput(['class'=>'form-control', 'placeholder'=>'parol', 'autofocus' => true])->label(false) ?>
                                </div>
                            </div>
                            <div class="col-md-6 pl-1">
                                <div class="form-group">
                                    <label>Ismingiz</label>
                                    <?= $form->field($model, 'second_name')->textInput(['class'=>'form-control', 'placeholder'=>'parol', 'autofocus' => true])->label(false) ?>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Shahar</label>
                                    <input type="text" class="form-control" placeholder="Home Address" value="Bld Mihail Kogalniceanu, nr. 8 Bl 1, Sc 1, Ap 09">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-4 pr-1">
                                <div class="form-group">
                                    <label>Manzil</label>
                                    <input type="text" class="form-control" placeholder="City" value="Mike">
                                </div>
                            </div>
                            <div class="col-md-4 px-1">
                                <div class="form-group">
                                    <label>Tug'ilgan sana</label>
                                    <input type="text" class="form-control" placeholder="Country" value="Andrew">
                                </div>
                            </div>
                            <div class="col-md-4 pl-1">
                                <div class="form-group">
                                    <label>Tel raqam</label>
                                    <input type="number" class="form-control" placeholder="ZIP Code">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
                                </div>
                            </div>
                        </div>

                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card card-user">
                <div class="image">
                    <img src="../assets/img/bg5.jpg" alt="...">
                </div>
                <div class="card-body">
                    <div class="author">
                        <?= $form->field($model, 'avatar')->widget(FileInput::classname(), [
                                    'name' => 'avatar',
                                    'pluginOptions' => [
                                        'showCaption' => false,
                                        'showRemove' => false,
                                        'showUpload' => false,
                                        'browseClass' => 'btn btn-primary btn-block',
                                        'browseIcon' => '<i class="glyphicon glyphicon-camera"></i> ',
                                        'browseLabel' =>  Yii::t('common','Rasm tanlash'),
                                    ],
                                    'options' => ['accept' => 'image/*'],

                                ]);
                               ?>
                    </div>
                </div>
                <?php ActiveForm::end(); ?>
                <hr>
                <div class="button-container">
                    <button href="#" class="btn btn-neutral btn-icon btn-round btn-lg">
                        <i class="fab fa-facebook-f"></i>
                    </button>
                    <button href="#" class="btn btn-neutral btn-icon btn-round btn-lg">
                        <i class="fab fa-twitter"></i>
                    </button>
                    <button href="#" class="btn btn-neutral btn-icon btn-round btn-lg">
                        <i class="fab fa-google-plus-g"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

Code model SingupForm.php :
spoiler
<?php
namespace frontend\models;

use yii\base\Model;
use common\models\User;
use yii\web\UploadedFile;

/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $first_name;
    public $second_name;
    public $avatar;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['email', 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }
        
        $user = new User();
        $user->username = $this->username;
        $user->first_name = $this->first_name;
        $user->second_name = $this->second_name;
        $user->email = $this->email;

        $user->setPassword($this->password);
        $user->generateAuthKey();
        if(UploadedFile::getInstance($user, 'avatar') != null)
        {

            $user->avatar= UploadedFile::getInstance($user, 'avatar');
            $imageName = $user->avatar->basename.time();
            $imageName =md5(md5($imageName));
            if (file_exists($imageName.$user->avatar->extension)) {
                unlink("uploads/avatars/".$imageName.$user->avatar->extension);
            }
            $user->avatar->saveAs('uploads/avatars/'.$imageName.'.'.$user->avatar->extension);
            $user->avatar='uploads/avatars/'.$imageName.'.'.$user->avatar->extension;
        }
        else
        {
            $user->avatar='uploads/no_photo.png';
        }


        return $user->save() ? $user : null;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-08-13
@Baxa94

Good afternoon.
First, add new fields to the validation rules.
If the new fields are required, then do so . If not required, then use the 'string' validator, or the one that suits you best. ps Those fields that are not in the validation do not pass.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question