PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 16, 2022

[FIXED] Symfony - Add text in generated form

 February 16, 2022     forms, symfony, twig     No comments   

Issue

I'd like to do something quite simple, but I can't figure out how to manage it. I have a form:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

There are several text field in it. I'd like to "insert" some text (like <p>my Text</p>) between two text fields (let's say between name text field and description text field). Form are generated with form builder tool. I've tried something like:

$builder
    ->add('stuffName') // works well (text field 1)
    ->add('addedText', 'text', array('label' => 'my Text')) // Trouble here!
    ->add('stuffDescription'); // works well (text field 2)

But it generates a text field (and not a simple text). I don't care if the text is set in the form builder or directly in twig template... As long as it is between my two text fields. Any idea?

Thanks a lot!


Solution

Symfony forms contain only form fields. Any additional content you want has to be added by the template.

This means you'll have to output the form field-by-field. Your form, for example might look like this:

{{ form_start(form) }}
    {{ form_row(form.stuffName) }}

    <p>Your Text</p>

    {{ form_row(form.stuffDescription) }}
{{ form_end(form) }}

For more more information on how you can customize form rendering, please see the forms chapter in the Symfony documentation.



Answered By - jbafford
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing