Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add to Melodiia a trait for form build #30

Open
Nek- opened this issue Aug 10, 2019 · 2 comments
Open

[RFC] Add to Melodiia a trait for form build #30

Nek- opened this issue Aug 10, 2019 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Nek-
Copy link
Contributor

Nek- commented Aug 10, 2019

Building forms is kinda repetitive but it is the recommended way to use Melodiia.

See what happens here, and here, and in any custom controller.

We could provide the following trait to make things smoother, here is an example of implementation:

use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Zend\Json\Exception\RuntimeException;
use Zend\Json\Json;

trait GetFormHydratedWithJsonRequest
{
    private function getForm(Request $request, FormFactoryInterface $formFactory, string $type, $data = null): FormInterface
    {
        $form = $formFactory->createNamed('', $type, $data);
        try {
            $requestContent = Json::decode((string) $request->getContent(), Json::TYPE_ARRAY);
        } catch (RuntimeException $e) {
            throw new HttpException(Response::HTTP_BAD_REQUEST, 'Invalid input data', $e);
        }
        $form->submit($requestContent, Request::METHOD_PATCH !== $request->getMethod());

        return $form;
    }
}
@Nek- Nek- added the enhancement New feature or request label Aug 10, 2019
@Nek- Nek- added the good first issue Good for newcomers label Oct 1, 2019
@nimaamiri92
Copy link

Can explain more what you want?

@Nek-
Copy link
Contributor Author

Nek- commented Oct 28, 2019

Hello @ARC92

Controllers provided by Melodiia have repetitive code. This is also the case of custom controllers. The goal of this issue is to provide a trait to avoid this repetitive code. In the thread I suggest directly an implementation.

What's new since this issue is that we have a trait. I'm not sure ATM that it should be inside the function the same trait. Probably. Here it is: https://github.com/biig-io/Melodiia/blob/master/src/Crud/Controller/CrudControllerTrait.php

Nek- added a commit to Nek-/Melodiia that referenced this issue Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants