diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 073b2ace41..7f5e323975 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -373,47 +373,53 @@ {% block widget_attributes %} {% spaceless %} - id="{{ id }}" - name="{{ full_name }}" - {% if read_only %} readonly="readonly"{% endif %} - {% if disabled %} disabled="disabled"{% endif %} - {% if required %} required="required"{% endif %} - {% if max_length %} maxlength="{{ max_length }}"{% endif %} - {% if pattern %} pattern="{{ pattern }}"{% endif %} - {% for attrname, attrvalue in attr %} - {% if attrname in ['placeholder', 'title'] %} - {{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" - {% else %} - {% if attrvalue is sameas(true) %} - {{ attrname }}="{{ attrname }}" - {% elseif attrvalue is not sameas(false) %} - {{ attrname }}="{{ attrvalue }}" - {% endif %} - {% endif %} - {% endfor %} + id="{{ id }}" name="{{ full_name }}" + {%- if read_only %} readonly="readonly"{% endif -%} + {%- if disabled %} disabled="disabled"{% endif -%} + {%- if required %} required="required"{% endif -%} + {%- if max_length %} maxlength="{{ max_length }}"{% endif -%} + {%- if pattern %} pattern="{{ pattern }}"{% endif -%} + {%- for attrname, attrvalue in attr -%} + {{- " " -}} + {%- if attrname in ['placeholder', 'title'] -%} + {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}" + {%- elseif attrvalue is sameas(true) -%} + {{- attrname }}="{{ attrname }}" + {%- elseif attrvalue is not sameas(false) -%} + {{- attrname }}="{{ attrvalue }}" + {%- endif -%} + {%- endfor -%} {% endspaceless %} {% endblock widget_attributes %} {% block widget_container_attributes %} {% spaceless %} - {% if id is not empty %}id="{{ id }}" {% endif %} - {% for attrname, attrvalue in attr %} - {% if attrname in ['placeholder', 'title'] %} - {{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" - {% else %} - {% if attrvalue is sameas(true) %} - {{ attrname }}="{{ attrname }}" - {% elseif attrvalue is not sameas(false) %} - {{ attrname }}="{{ attrvalue }}" - {% endif %} - {% endif %} - {% endfor %} + {%- if id is not empty %}id="{{ id }}"{% endif -%} + {%- for attrname, attrvalue in attr -%} + {{- " " -}} + {%- if attrname in ['placeholder', 'title'] -%} + {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}" + {%- elseif attrvalue is sameas(true) -%} + {{- attrname }}="{{ attrname }}" + {%- elseif attrvalue is not sameas(false) -%} + {{- attrname }}="{{ attrvalue }}" + {%- endif -%} + {%- endfor -%} {% endspaceless %} {% endblock widget_container_attributes %} {% block button_attributes %} {% spaceless %} - id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif %} - {% for attrname, attrvalue in attr %}{{ attrname }}="{{ attrvalue }}" {% endfor %} + id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif -%} + {%- for attrname, attrvalue in attr -%} + {{- " " -}} + {%- if attrname in ['placeholder', 'title'] -%} + {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}" + {%- elseif attrvalue is sameas(true) -%} + {{- attrname }}="{{ attrname }}" + {%- elseif attrvalue is not sameas(false) -%} + {{- attrname }}="{{ attrvalue }}" + {%- endif -%} + {%- endfor -%} {% endspaceless %} {% endblock button_attributes %} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php index 63d16bd357..ac1077a205 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php @@ -1,6 +1,10 @@ -id="escape($id) ?>" -name="escape($full_name) ?>" -disabled="disabled" +id="escape($id) ?>" name="escape($full_name) ?>" disabled="disabled" $v): ?> - escape($k), $view->escape($v)) ?> - + +escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?> + +escape($k), $view->escape($k)) ?> + +escape($k), $view->escape($v)) ?> + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php index 11c22832a7..a6a5fa8db4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_widget_simple.html.php @@ -1,5 +1 @@ -value="escape($value) ?>" - block($form, 'widget_attributes') ?> -/> +block($form, 'widget_attributes') ?> value="escape($value) ?>" /> diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php index 210b84cad5..292dbb9aa8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php @@ -1,10 +1,14 @@ -id="escape($id) ?>" -name="escape($full_name) ?>" -readonly="readonly" +id="escape($id) ?>" name="escape($full_name) ?>" readonly="readonly" disabled="disabled" required="required" maxlength="escape($max_length) ?>" pattern="escape($pattern) ?>" $v): ?> - escape($k), $view->escape(in_array($v, array('placeholder', 'title')) ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?> - + +escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?> + +escape($k), $view->escape($k)) ?> + +escape($k), $view->escape($v)) ?> + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php index 2a8e979b7f..327925a537 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php @@ -1,2 +1,10 @@ -id="escape($id) ?>" - $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?> +id="escape($id) ?>" + $v): ?> + +escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?> + +escape($k), $view->escape($k)) ?> + +escape($k), $view->escape($v)) ?> + + diff --git a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php index ee9ed8f2a6..9e1fb8663a 100644 --- a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php @@ -729,4 +729,42 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest $this->assertEquals('', $html); } + + public function testWidgetContainerAttributes() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('class' => 'foobar', 'data-foo' => 'bar'), + )); + + $form->add('text', 'text'); + + $html = $this->renderWidget($form->createView()); + + // compare plain HTML to check the whitespace + $this->assertContains('
', $html); + } + + public function testWidgetContainerAttributeNameRepeatedIfTrue() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('foo' => true), + )); + + $html = $this->renderWidget($form->createView()); + + // foo="foo" + $this->assertContains('
', $html); + } + + public function testWidgetContainerAttributeHiddenIfFalse() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('foo' => false), + )); + + $html = $this->renderWidget($form->createView()); + + // no foo + $this->assertContains('
', $html); + } } diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index d2ed7711ba..fc8317e9e3 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -302,8 +302,9 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg ); } - public function testWidgetById() + public function testOverrideWidgetBlock() { + // see custom_widgets.html.twig $form = $this->factory->createNamed('text_id', 'text'); $html = $this->renderWidget($form->createView()); @@ -1891,4 +1892,82 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg $this->assertSame('
', $html); } + + public function testWidgetAttributes() + { + $form = $this->factory->createNamed('text', 'text', 'value', array( + 'required' => true, + 'disabled' => true, + 'read_only' => true, + 'max_length' => 10, + 'pattern' => '\d+', + 'attr' => array('class' => 'foobar', 'data-foo' => 'bar'), + )); + + $html = $this->renderWidget($form->createView()); + + // compare plain HTML to check the whitespace + $this->assertSame('', $html); + } + + public function testWidgetAttributeNameRepeatedIfTrue() + { + $form = $this->factory->createNamed('text', 'text', 'value', array( + 'attr' => array('foo' => true), + )); + + $html = $this->renderWidget($form->createView()); + + // foo="foo" + $this->assertSame('', $html); + } + + public function testWidgetAttributeHiddenIfFalse() + { + $form = $this->factory->createNamed('text', 'text', 'value', array( + 'attr' => array('foo' => false), + )); + + $html = $this->renderWidget($form->createView()); + + // no foo + $this->assertSame('', $html); + } + + public function testButtonAttributes() + { + $form = $this->factory->createNamed('button', 'button', null, array( + 'disabled' => true, + 'attr' => array('class' => 'foobar', 'data-foo' => 'bar'), + )); + + $html = $this->renderWidget($form->createView()); + + // compare plain HTML to check the whitespace + $this->assertSame('', $html); + } + + public function testButtonAttributeNameRepeatedIfTrue() + { + $form = $this->factory->createNamed('button', 'button', null, array( + 'attr' => array('foo' => true), + )); + + $html = $this->renderWidget($form->createView()); + + // foo="foo" + $this->assertSame('', $html); + } + + public function testButtonAttributeHiddenIfFalse() + { + $form = $this->factory->createNamed('button', 'button', null, array( + 'attr' => array('foo' => false), + )); + + $html = $this->renderWidget($form->createView()); + + // no foo + $this->assertSame('', $html); + } } diff --git a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php index 5c91195169..70e1c7ccbc 100644 --- a/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php @@ -506,4 +506,42 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest $this->assertEquals('
', $html); } + + public function testWidgetContainerAttributes() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('class' => 'foobar', 'data-foo' => 'bar'), + )); + + $form->add('text', 'text'); + + $html = $this->renderWidget($form->createView()); + + // compare plain HTML to check the whitespace + $this->assertContains('', $html); + } + + public function testWidgetContainerAttributeNameRepeatedIfTrue() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('foo' => true), + )); + + $html = $this->renderWidget($form->createView()); + + // foo="foo" + $this->assertContains('
', $html); + } + + public function testWidgetContainerAttributeHiddenIfFalse() + { + $form = $this->factory->createNamed('form', 'form', null, array( + 'attr' => array('foo' => false), + )); + + $html = $this->renderWidget($form->createView()); + + // no foo + $this->assertContains('
', $html); + } }