small / forms
Provide PHP class to validate api data.
Requires
- php: >=8.3
- small/collection: 2.2.*
Requires (Dev)
- doctrine/dbal: 3.*
- doctrine/orm: 3.*
- pestphp/pest: 2.*
- phpstan/phpstan: ^1.10
- rector/rector: 0.19.*
- symfony/http-kernel: 6.4.*
- symfony/validator: >=6.4
This package is not auto-updated.
Last update: 2024-05-12 13:37:21 UTC
README
About
This project provides a data validation forms.
Useful to validate entry api data.
It is compatible with Symfony constraints annotations
Install
composer require small/forms
Inline forms
You can easily create a form inside your controller action.
For example, we want to check that this entry data match our controller action :
\Small\Forms\FormBuilder::createInlineForm()
->addField(
'name',
new \Small\Forms\Field\Type\StringType(),
[new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
)->addField('age', new \Small\Forms\Field\Type\IntType())
;
try {
$form->fillFromSymfonyRequest($request)
->validate($messages = new \Small\Collection\Collection\StringCollection())
;
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}
$arrayData = $form->toArray();
Class forms
If you want to reuse your form, you can declare it as a class :
namespace App\Form;
class PersonForm extends \Small\Forms\AbstractForm
{
public function build(): self {
$this->addField(
'name',
new \Small\Forms\Field\Type\StringType(),
[new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
);
$this->addField('age', new \Small\Forms\Field\Type\IntType());
}
}
And simply use it in your controllers :
try {
$form = \Small\Forms\FormBuilder::createFromFormClass(App\Form\PersonForm::class)
->fillFromSymfonyRequest($request)
->validate($messages = new \Small\Collection\Collection\StringCollection())
;
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}
$arrayData = $form->toArray();
Standard objects with attributes
You can also create a form directly from object
namespace App\DTO;
use Small\Forms\Modifier\NullIfEmptyModifier;use Small\Forms\ValidationRule\ValidateNotEmpty;
use Small\Forms\ValidationRule\ValidateGreaterOrEqual;
class Person
{
#[ValidateNotEmpty]
private string $name;
#[ValidateGreaterThanOrEqual(18)]
#[NullIfEmptyModifier]
private int $age;
}
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Employe
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
#[Assert\NotBlank]
private string $name;
#[ORM\Column]
private int $age;
#[ORM\Column]
private int $idService;
#[ORM\ManyToOne(inversedBy: 'Service')]
#[ORM\JoinColumn(name: "idService", referencedColumnName: "id", nullable: true)]
private Service|null $service = null;
}
try {
$form = \Small\Forms\FormBuilder::createFromAdapter(
new \Small\Forms\Adapter\AnnotationAdapter($person)
)->validate($messages = new \Small\Collection\Collection\StringCollection())
->hydrate($employe = new App\Entity\Employe());
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}
$useCase->joinCompanyUseCase($employe);
$form->fillFromObject($employe)
->hydrate($person)
;
return new \Symfony\Component\HttpFoundation\JsonResponse($person);
The validation rules will be parsed from your entity class php attributes.
Symfony DTO or entities
You can also create a form from DTO object
namespace App\DTO;
use Symfony\Component\Validator\Constraints as Assert;
class Person
{
#[Assert\NotBlank]
private string $name;
#[Assert\GreaterThanOrEqual(18)]
private int $age;
}
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Employe
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
#[Assert\NotBlank]
private string $name;
#[ORM\Column]
private int $age;
#[ORM\Column]
private int $idService;
#[ORM\ManyToOne(inversedBy: 'Service')]
#[ORM\JoinColumn(name: "idService", referencedColumnName: "id", nullable: true)]
private Service|null $service = null;
}
try {
$form = \Small\Forms\FormBuilder::createFromAdapter(
new \Small\Forms\Adapter\SymfonyAnnotationAdapter($person)
)->validate($messages = new \Small\Collection\Collection\StringCollection())
->hydrate($employe = new App\Entity\Employe());
} catch (\Small\Forms\ValidationRule\Exception\ValidationFailException) {
return new \Symfony\Component\HttpFoundation\JsonResponse($messages, \Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST);
}
$useCase->joinCompanyUseCase($employe);
$form->fillFromObject($employe)
->hydrate($person)
;
return new \Symfony\Component\HttpFoundation\JsonResponse($person);
The validation rules will be parsed from your entity class php attributes.