DEV Community

Cover image for small/forms
sebk69
sebk69

Posted on

small/forms

Here is a small library to validate and transform api data.

It can read Symfony atrtributes and you can normalize input data with modifiers like this :



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;

}

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);
}


Enter fullscreen mode Exit fullscreen mode

You can check it on on my gitlab server : https://git.small-project.dev/lib/small-forms
And packagist : https://packagist.org/packages/small/forms

Top comments (0)