Skip to content

Commit

Permalink
Merge pull request #50 from Nek-/refacto/forms
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- authored Nov 26, 2021
2 parents 771334f + ba50ce0 commit 488a5b4
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 92 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Removed hard dependency to the form component (it is still required for many features)

### Removed
- Automatic extension of the Symfony DateTimeType. There's a good DateTimeType available, we do not need more.

## [0.7.3] 2020-12-18

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"friendsofphp/php-cs-fixer": "^2.17",
"phpspec/prophecy": "^1.8",
"twig/twig": "^2.5",
"symfony/symfony": "^4.3 || ^5.0",
"symfony/symfony": "^4.4 || ^5.0",
"doctrine/orm": "^v2.6.6",
"justinrainbow/json-schema": "^5.2",
"doctrine/annotations": "^1.8",
Expand Down
3 changes: 3 additions & 0 deletions couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ menu:
cruds:
text: CRUD Reference
relativeUrl: docs/crud-controllers.html
forms:
text: Forms Reference
relativeUrl: docs/form-types.html
3 changes: 3 additions & 0 deletions docs/config-reference.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Configuration Reference of Melodiia
===================================

Here is the whole melodiia configuration commented:

```yaml
melodiia:
apis:
Expand All @@ -15,3 +17,4 @@ melodiia:
form_extensions:
datetime: true
```
22 changes: 22 additions & 0 deletions docs/form-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Using Symfony form types with Melodiia
======================================

Melodiia provides an `AbstractType` that you should extends to define you own types.

In addition, it provides some types that are very specific to API needs:
- BooleanType
- DateTimeType
- CollectionType

Those types are simple and behave just like you can expect. Excepted for the CollectionType.

The CollectionType
------------------

This type is very similar to the CollectionType of Symfony but regardless the original one, it supports APIs.

Here is a list of options you can use on it:
- `allow_add` (default to `true`): Allow the user to add items to the collection
- `allow_delete` (default to `true`): Allow the user to remove items from the collection
- `entry_type` (default to `TextType::class`): A form type that represent a collection item
- `entry_options` (default to `[]`): Options of the `entry_type`
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class TodoType extends AbstractType

You can now use the `Create` controller from the CRUD controllers.

By default Symfony Forms are not well designed for API. That's why we created a new `AbstractType` that you should
extends. Melodiia also provide a set of type for APIs needs: https://github.com/swagindustries/Melodiia/tree/master/src/Form/Type
By default Symfony Forms are not well-designed for API. That's why we created a new `AbstractType` that you should
extend. Melodiia also provide a set of [form types for APIs needs](form-types.md).

```yaml
# routing.yaml
Expand Down
21 changes: 14 additions & 7 deletions features/crud.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ Feature:
"data": [
{
"id": 1,
"content": "foo"
"content": "foo",
"publishDate":"2050-01-02T00:00:00+00:00"
},
{
"id": 2,
"content": "bar"
"content": "bar",
"publishDate":"2050-01-02T00:00:00+00:00"
},
{
"id": 3,
"content": "baz"
"content": "baz",
"publishDate":"2050-01-02T00:00:00+00:00"
}
]
}
Expand All @@ -42,7 +45,8 @@ Feature:
Given I make a "POST" request on "/todos" with the content:
"""
{
"content": "hello"
"content": "hello",
"publishDate":"2050-01-02T00:00:00+00:00"
}
"""
And the last response contains:
Expand All @@ -56,7 +60,8 @@ Feature:
"""
{
"id": 1,
"content": "hello"
"content": "hello",
"publishDate":"2050-01-02T00:00:00+00:00"
}
"""

Expand All @@ -81,11 +86,13 @@ Feature:
"data": [
{
"id": 2,
"content": "bar"
"content": "bar",
"publishDate":"2050-01-02T00:00:00+00:00"
},
{
"id": 3,
"content": "baz"
"content": "baz",
"publishDate":"2050-01-02T00:00:00+00:00"
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion features/custom_responses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Feature:
"data": [
{
"id": 1,
"content": "foo"
"content": "foo",
"publishDate":"2050-01-02T00:00:00+00:00"
}
]
}
Expand Down
6 changes: 0 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('form_extensions')
->addDefaultsIfNotSet()
->children()
->booleanNode('datetime')->defaultTrue()->end()
->end()
->end()
->end()
;

Expand Down
3 changes: 0 additions & 3 deletions src/DependencyInjection/MelodiiaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public function load(array $configs, ContainerBuilder $container)
}

if (class_exists(Form::class)) {
$loader->load('form.yaml');

// The CRUD features requires forms as well as doctrine to be enabled
if ($container->hasAlias('melodiia.data_provider')) {
$loader->load('crud.yaml');
Expand All @@ -49,7 +47,6 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('melodiia.config', $config);
$this->disableFormExtensionIfNeeded($container, $config['form_extensions']);

// Autoconf
$container->registerForAutoconfiguration(FilterInterface::class)->addTag(self::TAG_CRUD_FILTER);
Expand Down
45 changes: 0 additions & 45 deletions src/Form/Extension/DateTimeExtension.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MelodiiaCollectionType extends AbstractType
class CollectionType extends AbstractType
{
/**
* {@inheritdoc}
Expand All @@ -34,22 +32,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addEventSubscriber($reorderInputDataListener);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_replace($view->vars, [
'allow_add' => $options['allow_add'],
'allow_delete' => $options['allow_delete'],
]);

if ($form->getConfig()->hasAttribute('prototype')) {
$prototype = $form->getConfig()->getAttribute('prototype');
$view->vars['prototype'] = $prototype->setParent($form)->createView($view);
}
}

/**
* {@inheritdoc}
*/
Expand All @@ -58,7 +40,6 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'entry_type' => TextType::class,
'entry_options' => [],
'delete_empty' => false,
Expand Down
6 changes: 0 additions & 6 deletions src/Resources/config/form.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions tests/Behat/Context/TodoContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ public function thereAreSome()

$todo1 = new Todo();
$todo1->setContent('foo');
$todo1->setPublishDate(new \DateTimeImmutable('2050-01-02'));
$todo2 = new Todo();
$todo2->setContent('bar');
$todo2->setPublishDate(new \DateTimeImmutable('2050-01-02'));
$todo3 = new Todo();
$todo3->setContent('baz');
$todo3->setPublishDate(new \DateTimeImmutable('2050-01-02'));

$entityManager->persist($todo1);
$entityManager->persist($todo2);
Expand All @@ -39,6 +42,7 @@ public function thereIsOneTodo($content)

$todo = new Todo();
$todo->setContent($content);
$todo->setPublishDate(new \DateTimeImmutable('2050-01-02'));
$entityManager->persist($todo);

$entityManager->flush();
Expand Down
15 changes: 15 additions & 0 deletions tests/TestApplication/src/Entity/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Todo implements MelodiiaModel
*/
private $content;

/**
* @ORM\Column(type="datetime_immutable")
*/
private $publishDate;

public function getId(): ?int
{
return $this->id;
Expand All @@ -38,4 +43,14 @@ public function setContent($content): void
{
$this->content = $content;
}

public function getPublishDate()
{
return $this->publishDate;
}

public function setPublishDate(?\DateTimeImmutable $publishDate): void
{
$this->publishDate = $publishDate;
}
}
9 changes: 8 additions & 1 deletion tests/TestApplication/src/Form/TodoType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
namespace TestApplication\Form;

use SwagIndustries\Melodiia\Form\AbstractType;
use SwagIndustries\Melodiia\Form\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
use TestApplication\Entity\Todo;

class TodoType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('content', TextType::class, ['constraints' => new NotBlank()]);
$builder
->add('content', TextType::class, ['constraints' => new NotBlank()])
->add('publishDate', DateTimeType::class, [
'constraints' => [new GreaterThan('today'), new NotBlank()],
])
;
}

public function configureOptions(OptionsResolver $resolver)
Expand Down

0 comments on commit 488a5b4

Please sign in to comment.