Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3 forms: how to set default value for a textarea widget

I want to set the value on a textarea widget. How i can set default value for textarea in Symfony 3 for TextType(input type="text") i can use value parameter but for textarea i can not!!!how i can set default value for textarea.

this is my buildForm

    public function buildForm(FormBuilderInterface $builder, array $options)
    { 
        $builder
        ->add('linkdin', TextType::class, array('attr' => array('placeholder' => 
'linkdin','class' => 'form-control width100','value' => 
MainPageType::$content1[0]['linkdin'])))

        ->add('addres', CKEditorType::class, array('attr' => array('required' =>
 'false','name'=>'editor1' ,'id' => 'editor1','class' => 'ckeditor','empty_data'
 => MainPageType::$content1[0]['addres'])))
        .
    .
like image 617
pedram shabani Avatar asked Dec 24 '22 10:12

pedram shabani


2 Answers

Assuming you are using Symfony 3.4, there's quite a good documentation for that.

Long story short, you should use data:

$builder->add('token', TextareaType::class, array(
    'data' => 'abcdef',
));

As the docs say:

The data option always overrides the value taken from the domain data (object) when rendering. This means the object value is also overriden when the form edits an already persisted object, causing it to lose its persisted value when the form is submitted.

like image 189
Alex Karshin Avatar answered Dec 26 '22 17:12

Alex Karshin


You can transfer the variable with data to the formType in controller like that

$form = $this->createForm(Form::class,$YourData);
like image 22
Kuba Zabielski Avatar answered Dec 26 '22 17:12

Kuba Zabielski