Skip to content

Commit ecf8a8a

Browse files
Merge pull request #12 from MGDSoft/master
Fix default name (+ humanize).
2 parents 536c2a3 + 8516994 commit ecf8a8a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

features/interactive.feature

+14
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ Feature: It is possible to interactively fill in a form from the CLI
7272
[color] => blue
7373
)
7474
"""
75+
76+
Scenario: Empty label
77+
When I run the command "form:empty_label" and I provide as input
78+
"""
79+
empty[enter]
80+
"""
81+
Then the command has finished successfully
82+
And the output should be
83+
"""
84+
Field name: Array
85+
(
86+
[fieldName] => empty
87+
)
88+
"""

src/Form/FormUtil.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ public static function type(FormInterface $form)
6363
*/
6464
public static function label(FormInterface $form)
6565
{
66-
return $form->getConfig()->getOption('label', $form->getName());
66+
$label = $form->getConfig()->getOption('label');
67+
68+
if (!$label) {
69+
$label = self::humanize($form->getName());
70+
}
71+
72+
return $label;
6773
}
6874

6975
/**
@@ -75,4 +81,16 @@ public static function isCompound(FormInterface $form)
7581
{
7682
return $form->getConfig()->getCompound();
7783
}
84+
85+
/**
86+
* Copied from Symfony\Component\Form method humanize.
87+
*
88+
* @param $text
89+
*
90+
* @return string
91+
*/
92+
private static function humanize($text)
93+
{
94+
return ucfirst(trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $text))));
95+
}
7896
}

test/Form/EmptyLabelType.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
class EmptyLabelType extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options)
12+
{
13+
$builder
14+
->add(
15+
'fieldName',
16+
TextType::class
17+
);
18+
}
19+
}

test/config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ services:
4242
tags:
4343
- { name: console.command }
4444

45+
empty_label_command:
46+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
47+
arguments:
48+
- Matthias\SymfonyConsoleForm\Tests\Form\EmptyLabelType
49+
- empty_label
50+
tags:
51+
- { name: console.command }
52+
4553
framework:
4654
form:
4755
csrf_protection: true

0 commit comments

Comments
 (0)