Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace api\models;
- use common\models\User;
- use backend\models\AuthAssignment;
- use Yii;
- use yii\base\Exception;
- use yii\base\Model;
- use yii\helpers\ArrayHelper;
- /**
- * Create user form
- */
- class UserForm extends Model
- {
- public $username;
- public $password;
- public $status;
- public $phone;
- public $created_at;
- public $updated_at;
- public $id;
- public $fio;
- public $gender;
- public $birthdate;
- public $description;
- public $avatar;
- private $model;
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['phone'], 'required'],
- [['status'], 'integer'],
- [['created_at', 'updated_at'], 'safe'],
- [['username'], 'string', 'max' => 255],
- [['phone'], 'string', 'max' => 50],
- ['phone', 'unique', 'targetClass' => User::class, 'filter' => function ($query) {
- if (!$this->getModel()->isNewRecord) {
- $query->andWhere(['not', ['id' => $this->getModel()->id]]);
- }
- }],
- ['password', 'required', 'on' => 'create'],
- ['password', 'string', 'min' => 6],
- ];
- }
- /**
- * @return User
- */
- public function getModel()
- {
- if (!$this->model) {
- $this->model = new User();
- }
- return $this->model;
- }
- /**
- * @param User $model
- * @return mixed
- */
- public function setModel($model)
- {
- $this->username = $model->username;
- $this->status = $model->status;
- $this->phone = $model->phone;
- $this->phone = $model->fio;
- $this->phone = $model->gender;
- $this->phone = $model->birthdate;
- $this->phone = $model->description;
- $this->phone = $model->avatar;
- $this->model = $model;
- return $this->model;
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'username' => 'Имя',
- 'password' => 'Пароль',
- 'phone' => 'Телефон',
- ];
- }
- /**
- * Signs user up.
- * @return User|null the saved model or null if saving fails
- * @throws Exception
- */
- public function save()
- {
- if ($this->validate()) {
- $model = $this->getModel();
- $isNewRecord = $model->getIsNewRecord();
- $model->username = $this->username;
- $model->phone = $this->phone;
- $model->fio = $this->fio;
- $model->gender = $this->gender;
- $model->birthdate = $this->birthdate;
- $model->description = $this->description;
- $model->avatar = $this->avatar;
- $model->status = $this->status;
- if ($this->password) {
- $model->setPassword($this->password);
- }
- if (!$model->save()) {
- throw new Exception('Model not saved');
- }
- }
- return $model;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment