small / forms
Provide PHP class to validate api data.
1.7.5
2024-12-25 20:14 UTC
Requires
- php: >=8.1
- small/collection: 2.*
Requires (Dev)
- pestphp/pest: 2.*
- phpstan/phpstan: ^1.10
- rector/rector: *
- symfony/validator: *
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\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
- ValidateAtLeastOneOf
- ValidateBoolean
- ValidateCallback
- ValidateChoice
- ValidateCountLessOrEqualThan
- ValidateCountGreaterThan
- ValidateCountLessOrEqualThan
- ValidateCountLessThan
- ValidateDateTime
- ValidateDecimal
- ValidateDivisibleBy
- ValidateEmail
- 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
- ValidateSequencialy
- ValidateString
- ValidateStringArray
- ValidateUnique
List of modifiers
- ArrayToCollectionModifier
- ExplodeModifier
- FalseIfEmptyModifier
- FormBooleanToPhpModifier
- ImplodeModifier
- LTrimModifier
- NullIfEmptyModifier
- RoundModifier
- RTrimModifier
- StringToDateTimeImmutableModifier
- StringToDateTimeModifier
- SubStrModifier
- ToLowerModifier
- ToUpperModifier
- TrimModifier
- UcFirstModifier
- UcWordsModifier