Dev
Dev

Reputation: 305

Yii2 activeform onchange label name for the same attribute value

In my dropdownlist, when I choose Percentage, the label of the 'vendorcommision'(next field) should be "Percentage". When I choose Amount, the label should be "Amount"

if($model->userType!="ADMIN" && $model->userType!="HOST"){
      echo $form->field($model, 'fixedOrPercentagevendor')
                    ->dropDownList(
                    [0=>'Amount',1=>'Percentage'],
                    [
                        'onchange'=>'if($(this).val() == 1){
                            $("#percentage").val("20");
                            }
                            else{
                            $("#percentage").val("1000");}'
                    ]);

    echo $form->field($model, 'vendorcommision')
                  ->label('Vendor Fee')
                  ->textInput(['type' => 'number','id' => 'percentage']);
    }

Upvotes: 0

Views: 744

Answers (1)

skaur
skaur

Reputation: 168

Here is how I would do it:

  1. Turn label off for 'vendorcommision' [->label(false)]
  2. Use a span for custom label, eg <span class="custom-label"></span>
  3. Add event listener to the dropdown list to change labels. It will be on the lines of:

    var onChangeFunc = function(event) {
        if($(fixedOrPercentagevendorSelector).val() == 1) {
            $('.custom-label').html('Percentage');
        } else {
            $('.custom-label').html('Amount');
        }
    }
    

Note: If you include I18N support to your project, you will probably have to save the labels in some variable and use them.

Upvotes: 1

Related Questions