Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Reference
  4. Types
  5. submit Field Type
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Inherited Options
    • attr
    • disabled
    • label
    • label_attr
    • translation_domain
    • validation_groups
  • Form Variables

submit Field Type

Edit this page

Warning: You are browsing the documentation for Symfony 2.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

submit Field Type

2.3

The submit type was introduced in Symfony 2.3.

A submit button.

Rendered as button submit tag
Inherited options
  • attr
  • disabled
  • label
  • label_attr
  • translation_domain
  • validation_groups
Parent type button
Class SubmitType

The Submit button has an additional method isClicked() that lets you check whether this button was used to submit the form. This is especially useful when a form has multiple submit buttons:

1
2
3
if ($form->get('save')->isClicked()) {
    // ...
}

Inherited Options

attr

type: array default: array()

If you want to add extra attributes to the HTML representation of the button, you can use attr option. It's an associative array with HTML attribute as a key. This can be useful when you need to set a custom class for the button:

1
2
3
$builder->add('save', 'button', array(
    'attr' => array('class' => 'save'),
));

disabled

type: boolean default: false

If you don't want a user to be able to click a button, you can set the disabled option to true. It will not be possible to submit the form with this button, not even when bypassing the browser and sending a request manually, for example with cURL.

label

type: string default: The label is "guessed" from the field name

Sets the label that will be displayed on the button. The label can also be directly set inside the template:

  • Twig
  • PHP
1
{{ form_widget(form.save, { 'label': 'Click me' }) }}
1
<?php echo $view['form']->widget($form['save'], array('label' => 'Click me')) ?>

label_attr

type: array default: array()

Sets the HTML attributes for the <label> element, which will be used when rendering the label for the field. It's an associative array with HTML attribute as a key. This attributes can also be directly set inside the template:

  • Twig
  • PHP
1
2
3
{{ form_label(form.name, 'Your name', {
       'label_attr': {'class': 'CUSTOM_LABEL_CLASS'}
}) }}
1
2
3
4
5
echo $view['form']->label(
    $form['name'],
    'Your name',
    array('label_attr' => array('class' => 'CUSTOM_LABEL_CLASS'))
);

translation_domain

type: string default: messages

This is the translation domain that will be used for any labels or options that are rendered for this button.

validation_groups

type: array default: null

When your form contains multiple submit buttons, you can change the validation group based on the button which was used to submit the form. Imagine a registration form wizard with buttons to go to the previous or the next step:

1
2
3
4
5
6
7
8
$form = $this->createFormBuilder($user)
    ->add('previousStep', 'submit', array(
        'validation_groups' => false,
    ))
    ->add('nextStep', 'submit', array(
        'validation_groups' => array('Registration'),
    ))
    ->getForm();

The special false ensures that no validation is performed when the previous step button is clicked. When the second button is clicked, all constraints from the "Registration" are validated.

See also

You can read more about this in the Form chapter of the book.

Form Variables

Variable Type Usage
clicked boolean Whether the button is clicked or not.
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Code consumes server resources. Blackfire tells you how

Code consumes server resources. Blackfire tells you how

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Denis Charrier, a Symfony contributor

Thanks Denis Charrier (@brucewouaigne) for being a Symfony contributor

2 commits • 18 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia