php - Yii2 - How to pass 'isNewRecord' to custom form? -


i have custom form createadminform admin in backend can create new admin user. created own form model can handle passsword. fill out username, nicename, email, password, select status (active, banned, deleted), , choose role (root, super, admin). enter password normal, , custom form encrypts it. - have checks root can create anything, super can create regular admins, , regular admins can not create other admins @ all.

i had working on last project, bypassed isnewrecord because created forms in respective view files (create.php , update.php) instead of them both using same _form.php file. isn't big deal, follow way yii things , _form.php file handling forms again, if possible.

since using own model createadminform instead of admin model (which spin off of user), not have access isnewrecord.

how can _form.php use isnewrecord custom createadminform model?

truncated createadminform:

class createadminform extends model {     public $nicename;     public $username;     public $email;     public $password;     public $role;     public $status;      public function createadmin()     {         if ($this->validate()) {             $admin = new admin();             $admin->nicename = $this->nicename;             $admin->username = $this->username;             $admin->email = $this->email;             $admin->role = $this->role;             $admin->status = $this->status;             $admin->setpassword($this->password);             $admin->generateauthkey();             if ($admin->save()) {                 return $admin;             }         }          return null;     } } 

_form.php :

<div class="admin-form">      <?php $form = activeform::begin(); ?>      <?= $form->field($model, 'username')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'nicename')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'email')->textinput(['maxlength' => true]) ?>      <?= $form->field($model, 'role')->dropdownlist(         $model->getuserroledropdown(),         ['prompt'=>' - admin role - ']     ) ?>      <?= $form->field($model, 'status')->dropdownlist(         $model->getuserstatusdropdown(),         ['prompt'=>' - admin status - ']     ) ?>      <?= (!$model->isnewrecord) ? html::a('change password!', ['admin/change-admin-password', 'id' => $model->id]) . '<br /><br />' : '' ?>      <div class="form-group">         <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?>     </div>      <?php activeform::end(); ?>  </div> 

one way make createadminform class work way want work make hold copy of admin class property , have work on that. way, create method accesses admin's isnewrecord property. quick example below

class createadminform extends model {     // needs filled in instance of admin. maybe through constructor or through dependency injection     protected $admin;      public function createadmin()     {         if ($this->validate()) {             $this->admin->nicename = $this->nicename;             $this->admin->username = $this->username;             $this->admin->email = $this->email;             .             .             .             return $this->admin->save()         }          return false;     }      public function isnewrecord()     {         return $this->admin->isnewrecord;     } } 

as another, more simpler way (since seems have answered question), have createadminform inherit admin gain access activerecord , baseactiverecord such getisnewrecord() method (and not have worry managing 2 objects)

class createadminform extends admin {     // createadminform specific code } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -