FormModelDescriber: set type to array with correct example for multiple choices form. (#1212)

* FormModelDescriber: set type to array with correct example for multiple choices form.

* Remove arrays examples and add test for multiple ChoiceType form

* Use of "items" for multiple ChoiceType form.
Detect "choices" type to determine property or items type.
Remove unused variable declaration.
This commit is contained in:
fredrocheron 2018-02-03 12:52:43 +01:00 committed by Guilhem N
parent 0012b9438a
commit 72010c1d98
3 changed files with 37 additions and 5 deletions

View File

@ -45,7 +45,6 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
}
$schema->setType('object');
$properties = $schema->getProperties();
$class = $model->getType()->getClassName();
@ -101,9 +100,19 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
}
if ('choice' === $blockPrefix) {
$property->setType('string');
if ($config->getOption('multiple')) {
$property->setType('array');
} else {
$property->setType('string');
}
if (($choices = $config->getOption('choices')) && is_array($choices) && count($choices)) {
$property->setEnum(array_values($choices));
$enums = array_values($choices);
$type = $this->isNumbersArray($enums) ? 'number' : 'string';
if ($config->getOption('multiple')) {
$property->getItems()->setType($type)->setEnum($enums);
} else {
$property->setType($type)->setEnum($enums);
}
}
break;
@ -130,7 +139,6 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
if ($config->getOption('multiple')) {
$property->setFormat(sprintf('[%s id]', $entityClass));
$property->setType('array');
$property->setExample('[1, 2, 3]');
} else {
$property->setType('string');
$property->setFormat(sprintf('%s id', $entityClass));
@ -157,6 +165,22 @@ final class FormModelDescriber implements ModelDescriberInterface, ModelRegistry
}
}
/**
* @param array $array
*
* @return bool true if $array contains only numbers, false otherwise
*/
private function isNumbersArray(array $array): bool
{
foreach ($array as $item) {
if (!is_numeric($item)) {
return false;
}
}
return true;
}
private function isBuiltinType(string $type): bool
{
return 0 === strpos($type, 'Symfony\Component\Form\Extension\Core\Type');

View File

@ -24,6 +24,7 @@ class DummyType extends AbstractType
{
$builder->add('bar', TextType::class, ['required' => false]);
$builder->add('foo', ChoiceType::class, ['choices' => ['male', 'female']]);
$builder->add('foz', ChoiceType::class, ['choices' => ['male', 'female'], 'multiple' => true]);
$builder->add('baz', CheckboxType::class, ['required' => false]);
$builder->add('bey', IntegerType::class, ['required' => false]);
}

View File

@ -228,6 +228,13 @@ class FunctionalTest extends WebTestCase
'type' => 'string',
'enum' => ['male', 'female'],
],
'foz' => [
'type' => 'array',
'items' => [
'type' => 'string',
'enum' => ['male', 'female'],
],
],
'baz' => [
'type' => 'boolean',
],
@ -235,7 +242,7 @@ class FunctionalTest extends WebTestCase
'type' => 'integer',
],
],
'required' => ['foo'],
'required' => ['foo', 'foz'],
], $this->getModel('DummyType')->toArray());
}