How to glue 2 different inputs

Hi.

I have a model LoginForm with 2 fields username and password. And have 2 points of login. First it’s basic (site/login) with 2 input fields which has name attributes username and password respectively. Second point it’s (site/index) which have 3 input fields with code, phone, password name attributes. I need somelike glue code and phone in username and send just username and password to my model.

How can I do this? Or maybe need extend from my LoginForm with code,phone inputs?

Thanks in advance.

In loginForm add additional variables public $code and public $phone then you can manipulate $code $phone in view like other variables from loginForm ($password, $username). So after submit you pass data in $code and $phone do something with these to prepare data for $username and after validation save it to data base.

I did that, but I get error - username required, because it’s set in rules();




//from model LoginForm

public function login()

    {

        if ($this->code !==null && $this->phone !== null) {

            //login from index page

            $this->username = $this->code . $this->phone;

        }


        if ($this->validate()) {

            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);

        } else {

            return false;

        }

    }



Check what is in variables when you execute function login() especially what is going on inside if() and username after this condition.

I think it’s unreachable statement, because first checks the rule() which saying username required.

Did this in site controller for awhile


 

....

$postData = Yii::$app->request->post();

    if (isset($postData['LoginForm']['code'], $postData['LoginForm']['phone'])) {

        //glue

        $postData['LoginForm']['username'] = $postData['LoginForm']['code'] . $postData['LoginForm']['phone'];

    }


    if ($model->load($postData) && $model->login()) {

        ....



If will be any suggestions, please let me know

Yes, first is controller. If you receive error in view you can do something like this:

  1. in form disable client validation, enable ajax validation

  2. in controller put:

if (Yii::$app->request->isAjax) {

//set scenario

//do validation

use: return \yii\widgets\ActiveForm::validate();

}