merged branch kriswallsmith/form/mv-humanize (PR #4645)

Commits
-------

c1e4166 moved create of default form label to view layer

Discussion
----------

move create of default form label to view layer

A small optimization if you provide custom labels in the view layer (i.e. `{{ form_label(form.name, 'Your name') }}`

```
Bug fix: no
Feature addition: no
Backwards compatibility break: yes
Symfony2 tests pass: yes
Fixes the following tickets: ~
Todo: ~
License of the code: MIT
Documentation PR: ~
```

---------------------------------------------------------------------------

by travisbot at 2012-06-24T14:45:17Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1694310) (merged 37f0b774 into 0d4b02e4).

---------------------------------------------------------------------------

by travisbot at 2012-06-24T15:03:44Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1694418) (merged c1e4166e into 0d4b02e4).
This commit is contained in:
Fabien Potencier 2012-07-01 22:38:07 +02:00
commit c0e4760b38
7 changed files with 38 additions and 14 deletions

View File

@ -460,6 +460,22 @@
{% endfor %}
```
* Creation of default labels has been moved to the view layer. You will need
to incorporate this logic into any custom `form_label` templates to
accommodate those cases when the `label` option has not been explicitly
set.
```
{% block form_label %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
{# ... #}
{% endblock %}
````
#### Other BC Breaks
* The order of the first two arguments of the methods `createNamed` and

View File

@ -91,6 +91,13 @@ class FormExtension extends \Twig_Extension
);
}
public function getFilters()
{
return array(
'humanize' => new \Twig_Filter_Function(__NAMESPACE__.'\humanize'),
);
}
public function isChoiceGroup($label)
{
return FormUtil::isChoiceGroup($label);
@ -364,3 +371,8 @@ class FormExtension extends \Twig_Extension
return $blocks;
}
}
function humanize($text)
{
return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text))));
}

View File

@ -225,6 +225,9 @@
{% if required %}
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{% endif %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock form_label %}

View File

@ -1,3 +1,4 @@
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
<?php if (!$compound) { $label_attr['for'] = $id; } ?>
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>

View File

@ -314,6 +314,11 @@ class FormHelper extends Helper
return trim($this->engine->render($template, $variables));
}
public function humanize($text)
{
return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text))));
}
public function getName()
{
return 'form';

View File

@ -112,7 +112,7 @@ class FormType extends AbstractType
'max_length' => $options['max_length'],
'pattern' => $options['pattern'],
'size' => null,
'label' => $options['label'] ?: $this->humanize($form->getName()),
'label' => $options['label'],
'multipart' => false,
'attr' => $options['attr'],
'label_attr' => $options['label_attr'],
@ -226,9 +226,4 @@ class FormType extends AbstractType
{
return 'form';
}
private function humanize($text)
{
return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text))));
}
}

View File

@ -213,14 +213,6 @@ class FormTypeTest extends TypeTestCase
$this->assertEquals('messages', $view['child']->getVar('translation_domain'));
}
public function testPassDefaultLabelToView()
{
$form = $this->factory->createNamed('__test___field', 'form');
$view = $form->createView();
$this->assertSame('Test field', $view->getVar('label'));
}
public function testPassLabelToView()
{
$form = $this->factory->createNamed('__test___field', 'form', null, array('label' => 'My label'));