PHP code example of precision-soft / symfony-json-form
1. Go to this page and download the library: Download precision-soft/symfony-json-form library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
precision-soft / symfony-json-form example snippets
declare(strict_types=1);
/*
* Copyright (c) Vivre
*/
namespace Acme\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\Dto\ProductEditDto;
use Acme\Form\ProductEditForm;
use Acme\Service\ProductEditService;
class ProductController extends AbstractController
{
public function edit(Request $request, ProductEditForm $productEditForm, ProductEditService $productEditService): Response
{
$id = $request->get('id');
if (Request::METHOD_POST === $request->getMethod()) {
/** @var ProductEditDto $dto */
$dto = $productEditForm->handleRequest($request);
$productEditService->save($dto);
} else {
$dto = $productEditService->createDto($id);
}
return $this->json(
[
'form' => $productEditForm->render($dto),
]
);
}
}
declare(strict_types=1);
/*
* Copyright (c) Vivre
*/
namespace Acme\Dto;
use PrecisionSoft\Symfony\JsonForm\Contract\DtoInterface;
class ProductEditDto implements DtoInterface
{
private int $id;
private string $status;
public function getId(): int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getStatus(): string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
}
declare(strict_types=1);
/*
* Copyright (c) Vivre
*/
namespace Acme\Form;
use Acme\Dto\ProductEditDto;
use PrecisionSoft\Symfony\JsonForm\Element\ArrayElement;
use PrecisionSoft\Symfony\JsonForm\Element\NumberElement;
use PrecisionSoft\Symfony\JsonForm\Form\Action;
use PrecisionSoft\Symfony\JsonForm\Form\Form;
use PrecisionSoft\Symfony\JsonForm\Service\Contract\AbstractFormService;
class ProductEditForm extends AbstractFormService
{
protected function getDtoClass(): string
{
return ProductEditDto::class;
}
protected function getAction(): Action
{
return new Action('product-edit');
}
protected function build(Form $form): void
{
$form->addElement(new NumberElement('id', 'Id'))
->addElement(new ArrayElement('status', 'Status', ['active' => 'Active', 'inactive' => 'Inactive']));
}
}
declare(strict_types=1);
/*
* Copyright (c) Vivre
*/
namespace Acme\Service;
use Acme\Dto\ProductEditDto;
class ProductEditService
{
public function createDto(int $id): ProductEditDto
{
$dto = new ProductEditDto();
$dto->setId($id);
/* @todo populate all the data from the db */
return $dto;
}
public function save(ProductEditDto $dto): void
{
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.