@df12

Записываются пустые значение?

Здравствуйте,

WuldgrD.png

Код
RegistrationForm.php
<?php
 
namespace app\models;
 
use Yii;
use yii\base\Model;
 
/**
 * Registration form
 */
class RegistrationForm extends Model
{
	public $email;
	public $password;
	
	/**
	 * {@inheritdoc}
	 */
	public function rules()
	{
		return [
			['email', 'trim'],
			['email', 'required'],
			['email', 'email'],
			['email', 'string', 'max' => 255],
			['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],
			['password', 'required'],
			['password', 'string', 'min' => 6],
		];
	}

	/**
	 * {@inheritdoc}
	 */
	public function attributeLabels()
	{
		return [
			'login' => 'Логин',
			'password' => 'Пароль',
		];
	}
	
	/**
	 * Signs user up.
	 *
	 * @return User|null the saved model or null if saving fails
	 */
	public function save()
	{	
		if (!$this->validate()) {
			return null;
		}

		$user = new User();
		$user->email = $this->email;
		$user->setPassword($this->password);
		$user->generateAuthKey();
		return $user->save() ? $user : null;
	}
}


User.php
<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;

class User extends ActiveRecord implements IdentityInterface
{
	const STATUS_BANNED = 0;
    const STATUS_MEMBER = 1;
	const STATUS_STAFF  = 3;
	const STATUS_OWNER  = 5;

	public $id;
	public $email;
	public $password;
	public $authKey;
	// public $accessToken;

	/**
	 * {@inheritdoc}
	 */
	public static function tableName()
	{
		return '{{%user}}';
	}

	/**
	 * {@inheritdoc}
	 */
	public function behaviors()
	{
		return [
			TimestampBehavior::className(),
		];
	}

	/**
	 * {@inheritdoc}
	 */
	public function rules()
	{
		return [
			['status', 'default', 'value' => self::STATUS_MEMBER],
			['status', 'in', 'range' => [self::STATUS_OWNER, self::STATUS_STAFF, self::STATUS_MEMBER, self::STATUS_BANNED]],
		];
	}

    /**
     * {@inheritdoc}
     */
	public static function findIdentity($id)
	{
		return static::findOne(['id' => $id, 'status' => self::STATUS_MEMBER]);
	}

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
		throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
	 * Finds user by email
	 *
	 * @param string $email
	 * @return static|null
	 */
	public static function findByEmail($email)
	{
		return static::findOne(['email' => $email, 'status' => self::STATUS_MEMBER]);
	}

    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        // return $this->id;
		return $this->getPrimaryKey();
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return bool if password provided is valid for current user
     */
    public function validatePassword($password)
    {
		return Yii::$app->getSecurity()->validatePassword($password, $this->password);
    }

	/**
	 * Generates password hash from password and sets it to the model
	 *
	 * @param string $password
	 */
	public function setPassword($password)
	{
		$this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
	}

	/**
	 * Generates "remember me" authentication key
	 */
	public function generateAuthKey()
	{
		$this->authKey = Yii::$app->getSecurity()->generateRandomString();
	}
}


Контроллер
/**
	 * Registration action.
	 *
	 * @return Response|string
	 */
	public function actionRegistration()
	{
		$model = new RegistrationForm();

		if ($model->load(Yii::$app->request->post()) && $model->validate()) {
			if ($user = $model->save()) {
				if (Yii::$app->getUser()->login($user)) {
					return $this->goHome();
				}
			}
		}

		return $this->render('registration', ['model' => $model]);
	}


Шаблон
<?php $form = ActiveForm::begin(['id' => 'registration-form']); ?>
	<?= $form->field($model, 'email')->textInput([/*'type' => 'email', */'autofocus' => true]) ?>
	<?= $form->field($model, 'password')->passwordInput() ?>
	<div class="form-group">
		<?= Html::submitButton('Зарегистрироваться', ['class' => 'btn btn-primary', 'name' => 'registration-button']) ?>
	</div>
<?php ActiveForm::end(); ?>
  • Вопрос задан
  • 31 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы