Stefan Hansch
Stefan Hansch

Reputation: 1590

How create event onchange for field ActiveForm

Yii 2

I have following field on form ActiveForm:

<div class="col-sm-4">
        <?= $form->field($model, 'amount',
            ['options' => ['class' => 'form-group form-inline'],
            'onchange' => "
                if($('#dss-move_type').val() === '0'){    
                    var num = $(this).val();
                    num = num <= 0 : num ? -num;
                    $(this).val(num);
                }"]) ?>
    </div>

But get error: Setting unknown property: yii\bootstrap\ActiveField::onchange

How correct add option event onchange for field ActiveForm?

Upvotes: 1

Views: 468

Answers (1)

Kalu
Kalu

Reputation: 571

This might not be the answer to your question, but it will do what you want. Add the following to your view file:

    $this->registerJs(
    "$('#element_id').on('change', function (){
        if($('#dss-move_type').val() === '0'){    
            var num = $(this).val();
            num = num <= 0 : num ? -num;
            $(this).val(num);
        }
    });"
);

Upvotes: 1

Related Questions