P
P
Pavel Ivanov2014-09-16 11:13:38
Yii
Pavel Ivanov, 2014-09-16 11:13:38

Why does Yii 2.0 loop over a form?

There is a login form:

<div data-role="page" id="login">
    <div data-role="header">
        <h2>Авторизация</h2>
    </div>
    <div role="main" class="ui-content jqm-content">
        <form role="form" action="/login" method="POST" class="userform">
            <div class="ui-grid-solo">
                <div class="ui-grid-a">
                    <label for="username">Имя</label>
                    <input type="text" name="login" id="login" value="" data-clear-btn="true" data-mini="true">
                    <label for="password">Пароль</label>
                    <input type="password" name="password" id="password" value="" data-clear-btn="true" autocomplete="off" data-mini="true">

                    <input type="checkbox" name="remind" id="remind" value="1">
                    <label for="remind">Запомнить меня</label>
                    <br>
                    <input type="submit" value="Логин" onclick="this.form.submit();">
                </div>
            </div>
        </form>
    </div>
</div>

I do this in SiteController:
public function actionIndex()
    {
        $r = Yii::$app->request;

        if (\Yii::$app->user->isGuest) {
            return $this->render('login');
        }
        else {
            return $this->render('index');
        }
    }

     public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->render('login');
        }
        


        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->render('about');
        } else {
            /*
            return $this->render('login', [
                'model' => $model,
            ]);
            */
            return $this->render('contact');
        }
    }

In LoginForm:
public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }

    /**
     * Finds user by 
     *
     * @return User|null
     */
    public function getUser()
    {
        if ($this->_user === false) {
            $this->_user =  User::findByUsername($this->username);
        }
        return $this->_user;
    }

In models/User:
public $username = 'admin';
    public $password = 'admin';
    public $rememberMe = true;

    public function findByUsername($username) {
        if ($this->$username === $username) {
            return true;
        }
        else {
            return false;
        }

    }

    public function validatePassword($password)
    {
        if ($this->$username === $username) {
            return true;
        }
        else {
            return false;
        }
        //return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

Why does it throw me back into the Login form on submit?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Likhachev, 2014-09-16
@Playbot

actionLogin says that if not a guest, then render the login form

if (!\Yii::$app->user->isGuest) {
            return $this->render('login');
        }

P
Pavel Solovyov, 2014-09-16
@pavel_salauyou

do a form check in a separate action, from there make a redirect to where you need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question