0
votes

I have build a customized login form in yii2, the source code looks like this:

<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LoginForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;

$this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="card-body login-card-body">
    <p class="login-box-msg">Вход</p>


    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'layout' => 'horizontal',
        'method' => 'post',
        'fieldConfig' => [
            'options' => [
                'tag' => false,
            ],
             'template' =>'{input}'
        ],
    ]); ?>
        <div class="form-group has-feedback">
            <?= $form->field($model, 'username')->textInput(['class' => 'form-control',
                'placeholder' => 'Логин']); ?>
            <span class="fa fa-user form-control-feedback"></span>
            <p class="help-block help-block-error "></p>
        </div>
        <div class="form-group has-feedback">
            <?= $form->field($model, 'password')->passwordInput(['class' => 'form-control',
                'placeholder' => 'Пароль']); ?>
            <span class="fa fa-lock form-control-feedback"></span>
            <p class="help-block help-block-error "></p>
        </div>
         <div class="row">
             <div class="col-8">
             <?= $form->field($model, 'captcha')->widget(Captcha::className(),
                 ['captchaAction' => 'panel/captcha','template' => '<div class="captcha_img"><a href="#">{image}</a>
                    </div>'
                     . 'Капча{input}',
                 ])->label(FALSE); ?>
             </div>

         </div>
    <br>
        <div class="row">
            <div class="col-8">
                <div class="checkbox icheck">
                    <label>
                        <input type="checkbox"> Запомнить
                    </label>
                </div>
            </div>
            <!-- /.col -->
            <div class="col-4">

                <?= Html::submitButton('Вход', ['class' => 'btn btn-primary btn-block btn-flat',
                    'name' => 'login-button']) ?>

            </div>
            <!-- /.col -->
        </div>

        <?php ActiveForm::end(); ?>

</div>

As you can see i have ActiveForm stripped of auto generated tags, and stripped off labels. I have a standard form model from yii2-base-app, which handles the auth, model:

<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class LoginForm extends Model
{
    public $username;
    public $password;
    public $rememberMe = true;
    public $captcha;

    private $_user = false;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password','captcha'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
            ['captcha', 'captcha', 'captchaAction'=> 'panel/captcha']
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();

            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     * @return bool whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
        }
        return false;
    }

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

        return $this->_user;
    }
}

The login works fine, however $this->addError($attribute, 'Incorrect username or password.'); this line doesn't really work. When the debugger hits it nothing happens in the browser, if i put invalid login/pass no error messages added to the input. How can i fix this?

1

1 Answers

0
votes

This is the culprit: 'template' =>'{input}'. Refer to the Yii 2 documentation.

When you have the template defined, this:

<?= $form->field($model, 'password')->passwordInput(['class' => 'form-control', 'placeholder' => 'Пароль']); ?> 

is all you need. The markup you are trying to define on you view file will be rendered for you.