简体   繁体   中英

symfony 2 create custom form how to get form custom element?

$form = $this->createForm(new OrganizationType(), $entity, array(
        'action' => $this->generateUrl('_control_organization_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;

there is action and method defined. how to get this? in template engine twig into custom render?

By calling,

{{ form(form) }}

Or,

{{ form_start(form) }}

the action and method options values you added to your form definition are used.

From the documentation ...

Also, check Building The Form section of the documentation to see how to render the HTML form by passing

array('form' => $form->createView()) 

to the render helper within your controller.

Then, take a look at Rendering the Form part of the same documentation .

Also ...

If you want to override them in your template, you've to pass the right values to your form() or form_start() helpers as follow,

{{ form(form, {'action': path('target_route'), 'method': 'GET'}) }}

{{ form_start(form, {'action': path('target_route'), 'method': 'GET'}) }}
Return your form like this

return $this->render('Your Bundle name: Your controller name : Your twig file', array(
            'form' => $form->createView(),
        ));


And in your twig file get the form like this:

{{ form(form) }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM