Teshan N.
Teshan N.

Reputation: 2535

Render fields separately in HTML QuickForm2

I have been working on a form made using HTML_QuickForm2 in Symfony and I only know to render and display the entire form at once on Twig file. I need to separately display each field on the Twig layout.

Form

namespace AV\AppBundle\Form;

class ContactForm extends \HTML_QuickForm2 {

    function __construct() {
        parent::__construct('contactform', 'post', array('action' => '', 'name' => "form"));

        $first_name = $fs1->addText('first_name');
        $first_name->setLabel('First Name');
        $first_name->addRule('required', 'This field is required.');

        $last_name = $fs1->addText('last_name');
        $last_name->setLabel('Last Name');
        $last_name->addRule('required', 'This field is required.');

        $company = $fs1->addText('company');
        $company->setLabel('Company');

        $address = $fs1->addTextarea('address');
        $address->setLabel('Address');
        $address->addRule('required', 'This field is required.');

        $city = $fs1->addText('city');
        $city->setLabel('City');
        $city->addRule('required', 'This field is required.');
    }
}

Controller

public function contactAction() {
    $form = new \AV\AppBundle\Form\ContactForm();
    return $this->render('AVAppBundle:Contact:index.html.twig', array('form'=>$form));
}

Twig

{{form|raw}}

This form outputs the HTML content of the entire form with all fields at once. But I need to display each field separately in different places of the Twig/HTML page. How can I do this?

Upvotes: 0

Views: 76

Answers (0)

Related Questions