Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[Form] Allow to inherit translation domain for all elements of a form #3649

Closed
wants to merge 2 commits into from

4 participants

@uwej711

Currently I can set a translation_domain on each field of a form, but there is no simple way to do it once for a complete form. This change passes on the translation_domain from the form to its elements if translation_domain is not explicitly set on a field.

@stof
Collaborator

@bschussek what do you think about this way to do this ? I personally find it a bit hacky to add the option in the factory (and potentially broken if someone register a type which does not inherit from field btw) but I don't have a better idea

@uwej711 your branch needs to be rebased as tests have been moved

uwej711 added some commits
@uwej711 uwej711 Allow to inherit translation domain for all elements of a form
Currently I can set a translation_domain on each field of a form, but there is no simple way to do it once for a complete form. This change passes on the translation_domain form the form to its elements if translation_domain is not explicitly set on a field
ccb823c
@uwej711 uwej711 Tests have been moved 1f2196e
@uwej711

@stof rebase is done (had to force the push ...)

What is confusing about the form component is that there are two hierarchies one that builds up the form from the fields and one that is about types inheriting from other types. There are always parents involved and it is hard for the beginner to figure out what is what.

Currently I think there is no generic mechanism to propagate settings from the form (now I mean the first mentioned hierarchy) to the form elements. Maybe there are other cases where this would be useful, maybe the "error_bubbling" when I don't want to have the error messages at the field level.

@webmozart
Collaborator

@uwej711 You are right about the confusion. But we cannot solve this right now, unfortunately.

Apart from that, this solution doesn't make sense. The parent of a form could change later, in this case the translation domain wouldn't get updated. Could you open a ticket and close this PR unless you find a better solution?

@uwej711

@bschussek can you point me to a simple example of a forms parent changing later to find a solution that works for that case as well?

@webmozart
Collaborator

@uwej711 Just pass a Form instance to the add() method of another Form instance

@uwej711

I had a look at the code again and basically there is no chance to place this somewhere, when you want to take care of @bschussek 's concern about adding parents later: even when it is placed somewhere in the view building step I could add another view as a parent later and there will be the same issue as mentioned above. I don't see another solution that could take care of that issue.

@uwej711

Doing this via buildViewBottomUp in my custom form type will not really work nicely as I can no longer find out, whether the translation_domain was set somewhere explicitly or was just the default when the form was created :-(

@uwej711

I think it could be handled in the buildViewBottomUp. Maybe it will be easier based on #3789

@fabpot
Owner

I have opened a ticket #3872

@fabpot fabpot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 4, 2012
  1. @uwej711

    Allow to inherit translation domain for all elements of a form

    uwej711 authored
    Currently I can set a translation_domain on each field of a form, but there is no simple way to do it once for a complete form. This change passes on the translation_domain form the form to its elements if translation_domain is not explicitly set on a field
  2. @uwej711

    Tests have been moved

    uwej711 authored
This page is out of date. Refresh to see the latest.
View
4 src/Symfony/Component/Form/FormFactory.php
@@ -296,6 +296,10 @@ public function createNamedBuilder($type, $name, $data = null, array $options =
}
}
+ if (!isset($passedOptions['translation_domain']) && null !== $parent && $parent->hasAttribute('translation_domain')) {
+ $options['translation_domain'] = $parent->getAttribute('translation_domain');
+ }
+
for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
$builder = $types[$i]->createBuilder($name, $this, $options);
}
View
31 src/Symfony/Component/Form/Tests/Fixtures/TestTranslationType.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Symfony\Component\Form\Tests\Fixtures;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilder;
+
+class TestTranslationType extends AbstractType
+{
+ public function buildForm(FormBuilder $builder, array $options)
+ {
+ $builder->add('field1', 'choice', array(
+ 'choices' => array('1' => 'choice.1', '2' => 'choice.2'),
+ 'required' => true,
+ 'expanded' => true,
+ 'translation_domain' => 'field1'
+ ));
+ $builder->add('field2', 'checkbox', array('required' => true));
+ $builder->add('field3', 'text');
+ }
+
+ public function getDefaultOptions(array $options)
+ {
+ return array('translation_domain' => 'test');
+ }
+
+ public function getName()
+ {
+ return 'test_translation_type';
+ }
+}
View
13 src/Symfony/Component/Form/Tests/FormFactoryTest.php
@@ -535,6 +535,19 @@ public function testUnknownOption()
$factory->createNamedBuilder($type, "text", "value", array("unknown" => "opt"));
}
+ public function testInheritTranslationDomain()
+ {
+ $type = new \Symfony\Component\Form\Tests\Fixtures\TestTranslationType();
+ $factory = new FormFactory(array(new \Symfony\Component\Form\Extension\Core\CoreExtension()));
+
+ $builder = $factory->createNamedBuilder($type, 'test');
+ $view = $builder->getForm()->createView();
+
+ $this->assertSame('test', $view->get('translation_domain'));
+ $this->assertSame('field1', $view->getChild('field1')->get('translation_domain'));
+ $this->assertSame('test', $view->getChild('field2')->get('translation_domain'));
+ }
+
private function createMockFactory(array $methods = array())
{
return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
Something went wrong with that request. Please try again.