1

IS there anyway to add an uneditable field in magento 2 admin form?

$fieldset->addField(
            'giftcard_comment',
            'textarea',
            [
                'label' => __('Comment'),
                'title' => __('Comment'),
                'name' => 'giftcard_comment',
                'required' => false,
                'editable' => false
            ]
        );

2 Answers 2

5

You can set true and false by param value using this below way :

$id = $this->getRequest()->getParam('id', false);
$fieldset->addField(
    'giftcard_comment',
    'textarea',
    [
        'label' => __('Comment'),
        'title' => __('Comment'),
        'name' => 'giftcard_comment',
        'required' => false,
        'readonly' => $id ? true : false
    ]
);
3

Try 'readonly' => true insted of 'editable' => false

$id = $this->getRequest()->getParam('id');
$fieldset->addField(
            'giftcard_comment',
            'textarea',
            [
                'label' => __('Comment'),
                'title' => __('Comment'),
                'name' => 'giftcard_comment',
                'required' => false,
                'readonly' => $id ? true : false
            ]
        );

Hope this will help you.

3
  • My is issue is that I want the field disabled only on edit form not on create new form. If I make it readonly then it'll get disabled on create form as well!! Feb 3, 2020 at 12:00
  • Get params and check with inline if condition. if you get param then it's true otherwise it's false. I have updated my code please review it.
    – MK Patel
    Feb 3, 2020 at 12:03
  • Thanks for your support, but Rohan Hapani gave the perfect answer first. Therefore I marked it as the correct answer. Much appreciated Feb 3, 2020 at 12:38

Your Answer

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

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