Yii2 Mask Not working for form fields declared as array

 I have a form field like this
<?= $form->field($model, 'fee[]', ['options'=>['class'=>'service-mask-input text-right']])
    ->widget(MaskedInput::classname(), [
        'options'=>['class'=>"form-control text-right",  'tabindex' =>  67, 'maxlength' => 3],
        'clientOptions' => [
            'alias' => 'decimal',
            'digits' => 0,
            'digitsOptional' => false,
            'autoUnmask'=> true,
            'radixPoint' => '.',
            'groupSeparator' => ',',
            'autoGroup' => true,
            'prefix' => '$',
            'removeMaskOnSubmit' => true,
        ]
    )->textInput(['value'=>$serviceLists['fee'],'class'=>'service-mask-input'])

Here mask shows only for first text field.How to solve this issue?

Hi,

Sorry I don’t really get it. Your explanation is quite vague.

if you declare only 1

<?= $form->field($model ......

it seems normal that you will get just one text field.

what do you expect exactly ?

And you’ve forgot to close one bracket in you form declaration.

<?= $form->field($model, 'fee[]', ['options'=>['class'=>'service-mask-input text-right']])
    ->widget(MaskedInput::classname(), [
        'options'=>['class'=>"form-control text-right",  'tabindex' =>  67, 'maxlength' => 3],
        'clientOptions' => [
            'alias' => 'decimal',
            'digits' => 0,
            'digitsOptional' => false,
            'autoUnmask'=> true,
            'radixPoint' => '.',
            'groupSeparator' => ',',
            'autoGroup' => true,
            'prefix' => '$',
            'removeMaskOnSubmit' => true,
        ],])->textInput(['value'=>$serviceLists['fee'],'class'=>'service-mask-input']) ;
?>

Sorry for the late reply
Here I declared text fields as array .ie,fee[].Please see my screenshot here.

Here Mask for $ symbol display only for the first text field. I declared this text field as array fee[]

These text boxes are dynamically loaded from the table.

For a single text box, it is easy to apply the mask. But when declared as an array it will not work for all text boxes.

How can I solve this issue?

Thanks & Regards
Maya A

Hi, can you as well show us the code of your form ? I mean the whole form :slight_smile:

Hi,
Here is my code.Kindly check and update

<?php $form = ActiveForm::begin(['id'=>'add-room-service-tenant-details-form' , 'method' => 'post' , 'enableClientValidation'=>true , 'enableAjaxValidation' => false , 'layout' => 'horizontal' , 'action' => ['add-room-related-services','id' => $roomId,'id1'=>$tenantId] , 'fieldConfig'=>[ 'horizontalCssClasses' => ['wrapper' => '', 'offset'=>'']] , 'options'=>['class' => '' ,'enctype'=>'multipart/form-data' ,'data-pjax' => false] ]); ?>
Service
<?php $i=1; foreach($serviceList as $serviceLists){?> <?php //RoomService::widget(['serviceId'=>$serviceLists['id'],'form'=>$form,'model'=>$model,'serviceListFee'=>$serviceLists['fee']]);?> <?= $form->field($model, 'serviceId[]')->hiddenInput(['value'=> $serviceId],['class'=>'service-id-room-service-widget'.$serviceId])->label(false); ?> <?php //echo $form->field($model, 'service_id')->checkbox()->label('');?> <?= $form->field($model, 'inputcheckvalue[]')->hiddenInput(['value'=> $serviceId])->label(false); ?> <?php $i++; }?>
<?= Html::button(' Add Service',['class'=>'btn btn-info btn-sm btn-block text-white font-weight-bold site-btn' , 'id'=>'add-more-services-button-click']); ?>
# Services Fee Status
<?php echo $i;?> <?php echo $serviceLists['service_name']?> <?= $form->field($model, 'agreement')->hiddenInput(['value'=> $agreement])->label(false); ?> **<?= $form->field($model, 'fee[]'** **, ['options'=>['class'=>'service-mask-input text-right']** **])->widget(MaskedInput::classname(), ['options'=>['class'=>"form-control text-right", 'tabindex' => 67, 'maxlength' => 13],** **'clientOptions' => [** **'alias' => 'decimal',** **'digits' => 0,** **'digitsOptional' => false,** **'autoUnmask'=> true,** **'radixPoint' => '.',** **'groupSeparator' => ',',** **'autoGroup' => true,** **'prefix' => '$',** **'removeMaskOnSubmit' => true,** **],** **])->textInput(['value'=>$serviceListFee,'class'=>'service-mask-input'])->label(false); ?>** <?= $form->field($model, 'checkboxActiveValue', ['template'=>'
{input}
'])->textInput(['class'=>"check-lg",'id'=>'service-id-checkbox-room-service-widget'.$serviceId,'type'=>'checkbox','checked'=>true])->label(false);?>
<?= Html::button('Save',['class'=>'btn btn-info btn-block text-white font-weight-bold site-btn' ,'name'=>'login-button', 'id'=>'add-tenant-services-details-form-button','value'=>'0']) ?>
<?php ActiveForm::end(); ?>

I’m sorry but it is totally unreadable.
Can you modify your post, and copy/paste properly your code using the blockquote to have something clear with proper indentation.

Hi,
Thank you very much for the reply. Here is my code. Here I used a widget for passing id.
If it is difficult to read the code, I attached a PHP file here. Kindly check and update

(Attachment mask.php is missing)

Hi,

Here is my code. Kindly check and update

<?php $form = ActiveForm::begin(['id'=>'add-room-service-tenant-details-form' , 'method' => 'post' , 'enableClientValidation'=>true , 'enableAjaxValidation' => false , 'layout' => 'horizontal' , 'action' => ['add-room-related-services','id' => $roomId,'id1'=>$tenantId] , 'fieldConfig'=>[ , 'horizontalCssClasses' => ['wrapper' => '', 'offset'=>'']] , 'options'=>['class' => '' ,'enctype'=>'multipart/form-data' ,'data-pjax' => false] ]); ?>
Service
<?php $i=0; foreach($serviceList as $serviceLists){?> <?= RoomService::widget(['serviceId'=>$serviceLists['id'], 'form'=>$form,'model'=>$model, 'serviceListFee'=>$serviceLists['fee']]);?> <?php $i++; }?>
<?= Html::button(' Add Service', ['class'=>'btn btn-info btn-sm btn-block text-white font-weight-bold site-btn' , 'id'=>'add-more-services-button-click']); ?>
# Services Fee Status
<?php echo $i;?> <?php echo $serviceLists['service_name']?> <?= $form->field($model, 'agreement')->hiddenInput(['value'=> $agreement])->label(false); ?>
<?= Html::button('Save',['class'=>'btn btn-info btn-block text-white font-weight-bold site-btn' ,'name'=>'login-button', 'id'=>'add-tenant-services-details-form-button','value'=>'0']) ?> <?php //echo Html::a('Next', ['/tenant/a/index'], ['class' => 'btn btn-info btn-md btn-block text-white font-weight-bold mt-3 mr-2']) ?>
<?php ActiveForm::end(); ?>

Room Service Widget Code

<?= $form->field($model, 'fee[]' , ['options'=>['class'=>'service-mask-input text-right'] ])->widget(MaskedInput::classname(), ['options'=>['class'=>"form-control text-right", 'tabindex' => 67, 'maxlength' => 13], 'clientOptions' => [ 'alias' => 'decimal', 'digits' => 0, 'digitsOptional' => false, 'autoUnmask'=> true, 'radixPoint' => '.', groupSeparator' => ',', 'autoGroup' => true, 'prefix' => '$', 'removeMaskOnSubmit' => true, ], ])->textInput(['value'=>$serviceListFee,'class'=>'service-mask-input'])->label(false); ?> <?= $form->field($model, 'serviceId[]')->hiddenInput(['value'=> $serviceId], ['class'=>'service-id-room-service-widget'.$serviceId])->label(false); ?> <?= $form->field($model, 'inputcheckvalue[]')->hiddenInput(['value'=> $serviceId])->label(false); ?> <?= $form->field($model, 'checkboxActiveValue', ['template'=>'
{input}
']) ->textInput(['class'=>"check-lg",

‘id’=>‘service-id-checkbox-room-service-widget’.$serviceId,
‘type’=>‘checkbox’,‘checked’=>true])->label(false);?>

Thanks & Regards
Maya A