Skip to content

Commit d2677a0

Browse files
committed
merged branch bschussek/issue5383 (PR #6528)
This PR was merged into the master branch. Discussion ---------- [2.3] [Form] Support buttons in forms Bug fix: no Feature addition: yes Backwards compatibility break: yes Symfony2 tests pass: yes Fixes the following tickets: #5383 Todo: - License of the code: MIT Documentation PR: symfony/symfony-docs#2489 The general idea of this PR is to be able to add buttons to forms like so: ```php $builder->add('clickme', 'submit'); ``` You can then check in the controller whether the button was clicked: ```php if ($form->get('clickme')->isClicked()) { // do stuff } ``` Button-specific validation groups are also supported: ```php $builder->add('clickme', 'submit', array( 'validation_groups' => 'OnlyClickMe', )); ``` The validation group will then override the one defined in the form if that button is clicked. This PR also introduces the disabling of form validation by passing the value `false` in the option `validation_groups`: ```php $builder->add('clickme', 'submit', array( 'validation_groups' => false, )); ``` The same can be achieved (already before this PR) by passing an empty array: ```php $builder->add('clickme', 'submit', array( 'validation_groups' => array(), )); ``` See the linked documentation for more information. Commits ------- faf8d7a [Form] Added upgrade information about setting "validation_groups" => false d504732 [Form] Added leading backslashes to @exceptionMessage doc blocks c8afa88 [Form] Removed deprecated code scheduled for removal in 2.3 36ca056 [Form] Simplified Twig code ce29c70 [Form] Fixed incorrect doc comment 0bc7129 [Form] Fixed invalid use of FormException 600007b [Form] The option "validation_groups" can now be set to false to disable validation. This is identical to setting it to an empty array. 277d6df [Form] Fixed concatenation operator CS (see 7c47e34928c39e4797edc841423237a16588395e) 7b07925 [Form] Changed isset() to array_key_exists() to be consistent with ParameterBag 7b438a8 [Form] Made submit buttons able to convey validation groups cc2118d [Form] Implemented support for buttons
2 parents a2b06d6 + c191e9a commit d2677a0

15 files changed

+33
-7
lines changed

Resources/config/form.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@
140140
<service id="form.type.url" class="Symfony\Component\Form\Extension\Core\Type\UrlType">
141141
<tag name="form.type" alias="url" />
142142
</service>
143+
<service id="form.type.button" class="Symfony\Component\Form\Extension\Core\Type\ButtonType">
144+
<tag name="form.type" alias="button" />
145+
</service>
146+
<service id="form.type.submit" class="Symfony\Component\Form\Extension\Core\Type\SubmitType">
147+
<tag name="form.type" alias="submit" />
148+
</service>
149+
<service id="form.type.reset" class="Symfony\Component\Form\Extension\Core\Type\ResetType">
150+
<tag name="form.type" alias="reset" />
151+
</service>
143152

144153
<!-- FormTypeHttpFoundationExtension -->
145154
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
@@ -154,5 +163,8 @@
154163
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
155164
<tag name="form.type_extension" alias="repeated" />
156165
</service>
166+
<service id="form.type_extension.submit.validator" class="Symfony\Component\Form\Extension\Validator\Type\SubmitTypeValidatorExtension">
167+
<tag name="form.type_extension" alias="submit" />
168+
</service>
157169
</services>
158170
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id="<?php echo $view->escape($id) ?>"
2+
name="<?php echo $view->escape($full_name) ?>"
3+
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
4+
<?php foreach ($attr as $k => $v): ?>
5+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
6+
<?php endforeach; ?>

Resources/views/Form/button_label.html.php

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<?php echo $view['form']->widget($form) ?>
3+
</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
2+
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>>
3+
<?php $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?>
4+
</button>

Resources/views/Form/field_enctype.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_errors.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_label.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_rest.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_row.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_rows.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

Resources/views/Form/field_widget.html.php

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo $view['form']->block($form, 'button_widget', array('type' => isset($type) ? $type : 'reset')) ?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo $view['form']->block($form, 'button_widget', array('type' => isset($type) ? $type : 'submit')) ?>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<tr>
2+
<td></td>
3+
<td>
4+
<?php echo $view['form']->widget($form) ?>
5+
</td>
6+
</tr>

0 commit comments

Comments
 (0)