Suhari Adi
Suhari Adi

Reputation: 51

Update form using create form with onchange events

back again to this forum.. start asking all yii2 lesson i am using _form for create and update.. i use event onchange to call another table that has relation name in create form.. the problem is when i use this form to update, a couple field still blank because there is no triggers event change. here is my form code

  <?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp");?>

  <?= $form->field($model, 'id_sm_wp')->widget(Select2::classname(), [
    'language' => 'id',
    'data' => $idnpwp,
    'options' => ['placeholder' => 'Select a NPWP ...'],
    'pluginOptions' => [
    'allowClear' => true
    ],

    'pluginEvents' => [
       'change'=>'function(event){
       var data_id = event.currentTarget.value;
       $.post("'.Url::to(['mfwp/lists']).'?id="+data_id,function(data){


       $("input#namas").val(data.nama_wp);$("#alamats").val(data.alamat_wp);        
       $("input#nips").val(data.nip_pendek);
       $("input#namaar").val(data.pejabat.nama)
            });
        }'
     ]

   ]);

?>
<?php   if ($this->action->id == "update"){           


        //what code should be

    }
 ?>
<label> Nama Wajib Pajak </label>

<?= Html::textInput('nama','', $options=['id' => 'namas','class'=>'form- 
control', 'style'=>'width:1140px;margin-left:0px']) ?>

<br>

<label> Alamat Wajib Pajak </label>

<?= Html::textArea('alamat','', $options=['id' => 'alamats','class'=>'form- 
control', 'style'=>'width:1140px;margin-left:0px']) ?>

<br>

<table >

<tr>
    <th>
        <label> NIP </label>            
        <?= Html::input('text','nip','', $options=['id' => 
           'nips','class'=>'form-control', 'style'=>'width:200px;margin- 
           left:0px']) ?>
    </th>
    <th>
        <label style='margin-left:100px'> Nama Account Representative 
        </label>
        <?= Html::input('text','nama','', $options=['id' => 
          'namaar','class'=>'form-control', 
          'style'=>'width:840px;margin-left:100px']) ?>
    </th>
</tr>

</table>
<br>

anyone has reference should i read to deal with this case..thx a lot

Upvotes: 0

Views: 109

Answers (1)

user12172533
user12172533

Reputation: 11

if i understand what you want then the best approach from my end will be to do it using setter's and getter's

ex.:

Replace this code

<?= Html::input('text','nip','', $options=['id' => 
       'nips','class'=>'form-control', 'style'=>'width:200px;margin- 
       left:0px']) ?>

With this code:

<?= $form->field($model, 'nip')->textInput() ?>

After that u need to add a setter and getter methods in this model ($model).

also add a private property $_nip Setter method should be:

public function setNip($value)
{
    $this->_nip = $value;
}

u need to handle the save logic. Getter method should be:

public function getNip($value)
{
    if(empty($this->_nip)){
        // Code to get the value from other Table
    }
    return $this->_nip;
}

finally don't forget to add the new attributes to the rules method so that the model set them in the model.

Upvotes: 1

Related Questions