Вот пример регистации
Модель:
<?php
namespace app\models;
use app\helpers\Time;
use yii\helpers\Html;
use \yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class Users extends ActiveRecord implements IdentityInterface{
public $verifyCode;
public $sex = 'm';
public static function tableName()
{
return 'users';
}
public function rules(){
return [
[['login', 'password'], 'required', 'on' => 'reg'],
['login', 'string', 'min' => 3, 'max' => 25, 'tooShort' => 'Минимальная длина логина 3 сим.', 'tooLong' => 'Максимальная длина логина 25 сим.', 'on' => 'reg'],
['password', 'string', 'min' => 6, 'max' => 50, 'tooShort' => 'Минимальная длина пароля 6 сим.', 'tooLong' => 'Максимальная длина пароля 50 сим.', 'on' => 'reg'],
['login', 'unique', 'targetAttribute' => 'login', 'message' => 'Такой логин уже занят', 'on' => 'reg'],
['sex', 'in', 'range' => ['m', 'w'], 'on' => 'reg'],
['verifyCode', 'captcha', 'captchaAction' => 'index/captcha', 'on' => 'reg']
];
}
public function attributeLabels(){
return [
'login' => 'Логин',
'password' => 'Пароль',
'sex' => 'Ваш пол',
'verifyCode' => 'Код с картинки'
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('findIdentityByAccessToken is not implemented.');
}
public function getId()
{
return $this->getPrimaryKey();
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
public function beforeSave($insert){
if ($this->isNewRecord){
$this->time_reg = Time::real();
$this->time_visit = Time::real();
$this->time_total = 0;
}
$this->login = Html::encode($this->login);
$this->password = \Yii::$app->security->generatePasswordHash($this->password);
return parent::beforeSave($insert);
}
}
Контроллер:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\Users;
use app\models\LoginForm;
use yii\filters\AccessControl;
use yii\helpers;
class IndexController extends Controller
{
public function actionRegistration()
{
$model = new Users();
$model->scenario = 'reg';
if ($model->load(Yii::$app->request->post()) && $model->save()){
return $this->redirect(['/']);
}
return $this->render('registration/index', [
'model' => $model
]);
}
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'height' => 20,
'width' => 90,
'padding' => 0,
'offset' => 3,
'transparent' => true
],
];
}
}
Представление:
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\Users */
$this->title = 'Регистрация';
$this->params['breadcrumbs'][] = $this->title;
?>
<h1 class="page-header"><?= Html::encode($this->title) ?></h1>
<div class="row">
<div class="col-lg-4">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'login') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => 'index/captcha',
'template' => '<div class="form-group input-group">{input}<span class="input-group-addon">{image}</span></div>',
]) ?>
<?= $form->field($model, 'sex')->radioList(['m' => 'Мужской', 'w' => 'Женский']) ?>
<div class="form-group">
<?= Html::submitButton('Регистрация', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
Как бы так, для представления можно написать чистый html код, но объясню нафиг такие телодвижения. Вот такая хрень
$form->field($model, 'login')
Включает в себя label, оформления поля от состояния - если ошибка дается класс error если всё хорошо success, далее вывод самой ошибки сразу под полем и возможность валидации клиента & сервер что является default или ajax & сервер.