我在yii2中构建了一个自定义的登录表单,源代码如下所示:
<?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>
正如您所看到的,我将ActiveForm剥离了自动生成的标签,并剥离了标签。我有一个来自yii2-base-app的标准表单模型,它处理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;
}
}
登录工作正常,但$this->addError($attribute, 'Incorrect username or password.');
此行无法正常工作。当调试器命中它时,浏览器中没有任何反应,如果我将无效登录/传递没有错误消息添加到输入中。我该如何解决这个问题?
答案 0 :(得分:0)
这是罪魁祸首:'template' =>'{input}'
。请参阅Yii 2 documentation。
如果您定义了template
,那么:
<?= $form->field($model, 'password')->passwordInput(['class' => 'form-control', 'placeholder' => 'Пароль']); ?>
就是你所需要的。您将尝试在视图文件上定义的标记将为您呈现。