small / forms-bundle
Provide PHP class to validate api data in symfony.
Requires
- php: >=8.3
- small/forms: 1.*
- symfony/framework-bundle: >=v7.1.0
- symfony/property-access: *
- symfony/serializer: *
- symfony/validator: *
- symfony/yaml: *
Requires (Dev)
- pestphp/pest: 2.*
- phpstan/phpstan: ^1.10
- rector/rector: 0.19.*
- symfony/test-pack: *
This package is auto-updated.
Last update: 2024-12-09 03:26:31 UTC
README
About
This project is a Symfony bridge for small/forms.
small/forms- bundle is a library to validate, normalize and denormalize api data.
This package bypass symfony normalizer.
Install
composer require small/forms-bundle
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\Form\FormBuilder::createInlineForm()
->addField(
'name',
new \Small\Forms\Form\Field\Type\StringType(),
[new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
)->addField('age', new \Small\Forms\Form\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\Form\AbstractForm
{
public function build(): self {
$this->addField(
'name',
new \Small\Forms\Form\Field\Type\StringType(),
[new \Small\Forms\ValidationRule\ValidateNumberCharsLessThan(256)]
);
$this->addField('age', new \Small\Forms\Form\Field\Type\IntType());
}
}
And simply use it in your controllers :
try {
$form = \Small\Forms\Form\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\Form\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\Form\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.
List of validation rules
- ValidateBoolean
- ValidateString
- ValidateStringArray
- ValidateUnique
- ValidateDateTime
- ValidateDivisibleBy
- ValidateEmail
- ValidateEmpty
- ValidateEqual
- ValidateFloat
- ValidateFloatArray
- ValidateGreater
- ValidateGreaterOrEqual
- ValidateInt
- ValidateIntArray
- ValidateIsFalse
- ValidateIsNull
- ValidateIsTrue
- ValidateJson
- ValidateLess
- ValidateLessOrEqual
- ValidateMatchRegex
- ValidateMixedArray
- ValidateNegativeNumber
- ValidateNotEmpty
- ValidateNotEqual
- ValidateNotMatchRegex
- ValidateNotNull
- ValidateNumberCharsBetween
- ValidateNumberCharLessThan
- ValidatePositiveNumber
- ValidateRange
- ValidateRequired
- ValidateChoice
- ValidateCallback
- ValidateAtLeastOneOf
- ValidateSequencialy
List of modifiers
- ArrayToCollectionModifier
- ExplodeModifier
- FalseIfEmptyModifier
- FormBooleanToPhpModifier
- ImplodeModifier
- LTrimModifier
- NullIfEmptyModifier
- RTrimModifier
- StringToDateTimeImmutableModifier
- StringToDateTimeModifier
- SubStrModifier
- ToLowerModifier
- ToUpperModifier
- TrimModifier
- UcFirstModifier
- UcWordsModifier