forestcitylabs / framework

A PSR compliant framework for building web applications.
https://forestcitylabs.github.io/framework/
GNU General Public License v3.0
0 stars 0 forks source link

Form handling, to be or not to be? #49

Open midnightLuke opened 4 weeks ago

midnightLuke commented 4 weeks ago

Should we adopt/create a form-handling library that builds and validates forms? I feel like libs like Formr attempt too much and are catered towards more casual programmers, while libraries like Symfony Forms are way too tightly integrated with the framework and have way too many features for our humble framework here.

The other big question here: do we do it at all? If we are targeting GraphQL APIs primarily then it might be overkill.

midnightLuke commented 4 weeks ago

I feel like the ideal API would look something like this:

<?php

$form = new Form();
$name = $form->addInput(new Input('name'));
$name->addValidator(function (string $data) {
    return is_string($data);
});
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    $name = $form->getData()['name'];
}

return $this->render('form.html.twig', ['form' => $form]);

This provides super simple validation, but this is obviously a pretty simple example.

midnightLuke commented 4 weeks ago

This library would strictly avoid presentation of the form and only handle the building and validation of the form on the server-side. We could create a twig plugin to render the form elements, however, we would not particularly want to implement things like class in our available attributes on our form API.