DEV Community

Cover image for small/forms
sebk69
sebk69

Posted on

2

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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Image of DataStax

AI Agents Made Easy with Langflow

Connect models, vector stores, memory and other AI building blocks with the click of a button to build and deploy AI-powered agents.

Get started for free