0

I added a text area in the admin form and want to show pre-defined text in that. So is it possible and How?

$fieldset->addField(
                'approval_note',
                'textarea',
                [
                'name' => 'approval_note',
                'label' => __('Thank you Note'),
                'title' => __('Thank you Note')
                ]
            );

I want to show text with the below data by default.

enter image description here

Need Help !!

3 Answers 3

0

Try with below code

$fieldset->addField(
    'approval_note',
    'textarea',
    [
    'name' => 'approval_note',
    'label' => __('Thank you Note'),
    'title' => __('Thank you Note'),
    'value' => 'Hello Symon,
How are you?
Thanks for contacting me.'
    ]
);
3
  • It's not working
    – Krupali
    May 13, 2021 at 11:22
  • Is it display anything or sets any value? May 13, 2021 at 12:06
  • No.. Its not working
    – Krupali
    May 13, 2021 at 12:07
0

For Custom Form

$fieldset->addField(
    'approval_note',
    'textarea',
    [
    'name' => 'approval_note',
    'label' => __('Thank you Note'),
    'title' => __('Thank you Note'),
    'values' => 'Default Value'
    ]
);

For System Config

You can give a default value to the Textarea field by creating a config.xml file.
Place your file in the below-mentioned path

namespace\modulename\etc\config.xml

and add code as below and change "Section Name" and "Group Name" according to your system.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <sectionname>
            <groupname>
                <description>Textarea default text</description>
            </groupname>
        </sectionname>
    </default>
</config>
5
  • The text area is not in the system -> configuration section.. It's in my custom form.
    – Krupali
    May 13, 2021 at 11:24
  • @Krupali sorry for the misunderstanding I'll change my answer. May 13, 2021 at 11:38
  • @Krupali can you check my answer now May 13, 2021 at 11:51
  • Define 'values' is not working.
    – Krupali
    May 13, 2021 at 12:07
  • Try replace values with - 'values' => 'Default Value' with 'value' => 'Default Value' May 13, 2021 at 13:09
0

If simple "Value" not working then you can add renderer as below.

add below code in your form block e.g main.php

 $fieldset->addType(
            'htmldata',
            '\Vendor\Module\Block\Adminhtml\Items\Renderer\Autofill'
  );
    
   $fieldset->addField(
            'approval_note',
            'htmldata',
            ['name' => 'approval_note', 'label' => __('Thank you Note'), 'title' => __('Product Cost Notes'), 'required' => false,'nl2br' => true,
                'escape' => true]
        );

add new file Autofill.php as per definded path in file "Vendor\Module\Block\Adminhtml\Items\Renderer" and add below code

<?php 
namespace Vendor\Module\Block\Adminhtml\Items\Renderer;
 
/**
* CustomFormField Customformfield field renderer
*/
class Autofill extends \Magento\Framework\Data\Form\Element\AbstractElement
{
    /**
    * Get the after element html.
    *
    * @return mixed
    */
    public function getElementHtml()
    {
        // here you can write your code.
        if($this->getValue()){
            $enquiryData = nl2br($this->getValue());
        }else{
        /*default value
            $enquiryData = "Your default text here";
        }
        return $enquiryData;
    }
}

accept as a solution and do upvote if its works for you.

Your Answer

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.