SubmitType Field
A submit button.
Rendered as | button submit tag |
Parent type | ButtonType |
Class | SubmitType |
Tip
The full list of options defined and inherited by this form type is available running this command in your app:
1 2
# replace 'FooType' by the class name of your form type
$ php bin/console debug:form FooType
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()) {
// ...
}
Options
validate
type: boolean
default: true
Set this option to false
to disable the client-side validation of the form
performed by the browser.
Inherited Options
attr
type: array
default: []
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 4 5 6
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
// ...
$builder->add('save', SubmitType::class, [
'attr' => ['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
or TranslatableMessage
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:
1
{{ form_widget(form.save, { 'label': 'Click me' }) }}
1
<?= $view['form']->widget($form['save'], ['label' => 'Click me']) ?>
label_format
type: string
default: null
Configures the string used as the label of the field, in case the label
option was not set. This is useful when using
keyword translation messages.
If you're using keyword translation messages as labels, you often end up having
multiple keyword messages for the same label (e.g. profile_address_street
,
invoice_address_street
). This is because the label is built for each "path"
to a field. To avoid duplicated keyword messages, you can configure the label
format to a static value, like:
1 2 3 4 5 6 7 8
// ...
$profileFormBuilder->add('address', AddressType::class, [
'label_format' => 'form.address.%name%',
]);
$invoiceFormBuilder->add('invoice', AddressType::class, [
'label_format' => 'form.address.%name%',
]);
This option is inherited by the child types. With the code above, the label of
the street
field of both forms will use the form.address.street
keyword
message.
Two variables are available in the label format:
%id%
-
A unique identifier for the field, consisting of the complete path to the
field and the field name (e.g.
profile_address_street
); %name%
-
The field name (e.g.
street
).
The default value (null
) results in a
"humanized" version of the field name.
Note
The label_format
option is evaluated in the form theme. Make sure to
update your templates in case you
customized form theming.
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.
label_translation_parameters
type: array
default: []
The content of the label option is translated before displaying it, so it can contain translation placeholders. This option defines the values used to replace those placeholders.
Given this translation message:
1 2
# translations/messages.en.yaml
form.order.submit_to_company: 'Send an order to %company%'
You can specify the placeholder values as follows:
1 2 3 4 5 6 7 8 9
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
// ...
$builder->add('send', SubmitType::class, [
'label' => 'form.order.submit_to_company',
'label_translation_parameters' => [
'%company%' => 'ACME Inc.',
],
]);
The label_translation_parameters
option of buttons is merged with the same
option of its parents, so buttons can reuse and/or override any of the parent
placeholders.
attr_translation_parameters
type: array
default: []
The content of the title
and placeholder
values defined in the attr
option is translated before displaying it, so it can contain
translation placeholders. This
option defines the values used to replace those placeholders.
Given this translation message:
1 2 3
# translations/messages.en.yaml
form.order.id.placeholder: 'Enter unique identifier of the order to %company%'
form.order.id.title: 'This will be the reference in communications with %company%'
You can specify the placeholder values as follows:
1 2 3 4 5 6 7 8 9
$builder->add('id', null, [
'attr' => [
'placeholder' => 'form.order.id.placeholder',
'title' => 'form.order.id.title',
],
'attr_translation_parameters' => [
'%company%' => 'ACME Inc.',
],
]);
The attr_translation_parameters
option of children fields is merged with the
same option of their parents, so children can reuse and/or override any of the
parent placeholders.
row_attr
type: array
default: []
An associative array of the HTML attributes added to the element which is used to render the form type row:
1 2 3
$builder->add('body', TextareaType::class, [
'row_attr' => ['class' => 'text-editor', 'id' => '...'],
]);
See also
Use the attr
option if you want to add these attributes to
the form type widget element.
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 9 10 11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
// ...
$form = $this->createFormBuilder($user)
->add('previousStep', SubmitType::class, [
'validation_groups' => false,
])
->add('nextStep', SubmitType::class, [
'validation_groups' => ['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 How to Choose Validation Groups Based on the Submitted Data.
Form Variables
Variable | Type | Usage |
---|---|---|
clicked | boolean |
Whether the button is clicked or not. |