Skip to content

[2.3] [Form] Support buttons in forms #6528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions UPGRADE-2.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
UPGRADE FROM 2.2 to 2.3
=======================

### Form

* Although this was not officially supported nor documented, it was possible to
set the option "validation_groups" to false, resulting in the group "Default"
being validated. Now, if you set "validation_groups" to false, the validation
of a form will be skipped (except for a few integrity checks on the form).

If you want to validate a form in group "Default", you should either
explicitly set "validation_groups" to "Default" or alternatively set it to
null.

Before:

```
// equivalent notations for validating in group "Default"
"validation_groups" => null
"validation_groups" => "Default"
"validation_groups" => false

// notation for skipping validation
"validation_groups" => array()
```

After:

```
// equivalent notations for validating in group "Default"
"validation_groups" => null
"validation_groups" => "Default"

// equivalent notations for skipping validation
"validation_groups" => false
"validation_groups" => array()
```
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@
{% endspaceless %}
{% endblock email_widget %}

{% block button_widget %}
{% spaceless %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
{% endspaceless %}
{% endblock button_widget %}

{% block submit_widget %}
{% spaceless %}
{% set type = type|default('submit') %}
{{ block('button_widget') }}
{% endspaceless %}
{% endblock submit_widget %}

{% block reset_widget %}
{% spaceless %}
{% set type = type|default('reset') %}
{{ block('button_widget') }}
{% endspaceless %}
{% endblock reset_widget %}

{# Labels #}

{% block form_label %}
Expand All @@ -237,6 +260,8 @@
{% endspaceless %}
{% endblock form_label %}

{% block button_label %}{% endblock %}

{# Rows #}

{% block repeated_row %}
Expand All @@ -259,6 +284,14 @@
{% endspaceless %}
{% endblock form_row %}

{% block button_row %}
{% spaceless %}
<div>
{{ form_widget(form) }}
</div>
{% endspaceless %}
{% endblock button_row %}

{% block hidden_row %}
{{ form_widget(form) }}
{% endblock hidden_row %}
Expand Down Expand Up @@ -317,14 +350,9 @@
{% endspaceless %}
{% endblock widget_container_attributes %}

{# Deprecated in Symfony 2.1, to be removed in 2.3 #}

{% block generic_label %}{{ block('form_label') }}{% endblock %}
{% block widget_choice_options %}{{ block('choice_widget_options') }}{% endblock %}
{% block field_widget %}{{ block('form_widget_simple') }}{% endblock %}
{% block field_label %}{{ block('form_label') }}{% endblock %}
{% block field_row %}{{ block('form_row') }}{% endblock %}
{% block field_enctype %}{{ block('form_enctype') }}{% endblock %}
{% block field_errors %}{{ block('form_errors') }}{% endblock %}
{% block field_rest %}{{ block('form_rest') }}{% endblock %}
{% block field_rows %}{{ block('form_rows') }}{% endblock %}
{% block button_attributes %}
{% spaceless %}
id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif %}
{% for attrname, attrvalue in attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %}
{% endspaceless %}
{% endblock button_attributes %}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
{% endspaceless %}
{% endblock form_row %}

{% block button_row %}
{% spaceless %}
<tr>
<td></td>
<td colspan="2">
{{ form_widget(form) }}
</td>
</tr>
{% endspaceless %}
{% endblock button_row %}

{% block hidden_row %}
{% spaceless %}
<tr style="display: none">
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@
<service id="form.type.url" class="Symfony\Component\Form\Extension\Core\Type\UrlType">
<tag name="form.type" alias="url" />
</service>
<service id="form.type.button" class="Symfony\Component\Form\Extension\Core\Type\ButtonType">
<tag name="form.type" alias="button" />
</service>
<service id="form.type.submit" class="Symfony\Component\Form\Extension\Core\Type\SubmitType">
<tag name="form.type" alias="submit" />
</service>
<service id="form.type.reset" class="Symfony\Component\Form\Extension\Core\Type\ResetType">
<tag name="form.type" alias="reset" />
</service>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to register the Validator SubmitButtonTypeExtension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can I use if $form->get('clickme') == null to check if this exist or not ?

if ($form->get('clickme') != null && $form->get('clickme')->isClicked()) {
// do stuff
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josephzhao $form->get() will throw an exception in case the child does not exist. This is why we have $form->has()


<!-- FormTypeHttpFoundationExtension -->
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
Expand All @@ -154,5 +163,8 @@
<service id="form.type_extension.repeated.validator" class="Symfony\Component\Form\Extension\Validator\Type\RepeatedTypeValidatorExtension">
<tag name="form.type_extension" alias="repeated" />
</service>
<service id="form.type_extension.submit.validator" class="Symfony\Component\Form\Extension\Validator\Type\SubmitTypeValidatorExtension">
<tag name="form.type_extension" alias="submit" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id="<?php echo $view->escape($id) ?>"
name="<?php echo $view->escape($full_name) ?>"
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
<?php endforeach; ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<?php echo $view['form']->widget($form) ?>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>>
<?php $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?>
</button>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $view['form']->block($form, 'button_widget', array('type' => isset($type) ? $type : 'reset')) ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $view['form']->block($form, 'button_widget', array('type' => isset($type) ? $type : 'submit')) ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<tr>
<td></td>
<td>
<?php echo $view['form']->widget($form) ?>
</td>
</tr>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testNormalizeThrowsExceptionWhenFalseIsNotAllowed()
}

/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage Unrecognized options "foo" under "root"
*/
public function testExceptionThrownOnUnrecognizedChild()
Expand Down
Loading