0

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

4
  • its really hard to understand what you ask, please explain correctly what are you trying to achieve in proper steps Oct 6, 2019 at 7:08
  • When iam update, value from table added to the model field.but on the html::input field is still blank.just because its triggered by onchange events. The value come from different table.how can fill this html::input field by using $form->field($model, 'id_sm_wp') as reference..
    – Suhari Adi
    Oct 6, 2019 at 7:12
  • which text input you want the value to be filled? and on update you should load the value in the text input that was saved earlier when you inserted the record, rather than using the onchange trigger of the select2 Oct 6, 2019 at 8:03
  • text input i want to fill for example is id:namas. yes my friend iam start thinking to create new form update and remove input text with no model.
    – Suhari Adi
    Oct 6, 2019 at 8:26

1 Answer 1

1

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.