3

IF I am embedding a form type Collection in a form. I have set allow_add to true. How is it possible to override the prototype output. Say I want to wrap the collection of form types in a table on each add. Figured I can play around with the prototype output in jquery but would much rather get this right from the start. As an example my collection is of type:

class EmailShareType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       $builder->add("shareName","text",array("label"=>"Recipient Name:"));
       $builder->add("emailName", "email", array("label"=>"Recipient Email:","required" => true));
       $builder->add("emailMessage", "textarea",array("label"=>"Enter email message", "max_length" => "4000"));
    }

    public function getName() 
    {
        return 'emailShare';
    }    
} 

and the resulting prototype is rendered as:

data-prototype="
<div>
<label class="required">__name__label__</label>
<div id="emailShareCollection_emailShares___name__">
<div>
<label for="emailShareCollection_emailShares___name___shareName" class="required">Recipient Name:</label>
<input type="text" id="emailShareCollection_emailShares___name___shareName" name="emailShareCollection[emailShares][__name__][shareName]" required="required" />
</div>
<div>
<label for="emailShareCollection_emailShares___name___emailName" class="required">Recipient Email:</label>
<input type="email" id="emailShareCollection_emailShares___name___emailName" name="emailShareCollection[emailShares][__name__][emailName]" required="required" />
</div>
<div>
<label for="emailShareCollection_emailShares___name___emailMessage" class="required">Enter email message</label>
<textarea id="emailShareCollection_emailShares___name___emailMessage" name="emailShareCollection[emailShares][__name__][emailMessage]" required="required" maxlength="4000"></textarea>
</div>
</div>
</div>"

How do I get control over this output?

1

1 Answer 1

5

You can render it as you want by passing a custom template :

data-prototype="{% filter escape %}{% include 'YourBundle:YourController:yourTemplate.html.twig' with {'form': form.collectonField.get('prototype')} %}{% endfilter %}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.