PhpFormGenerator est un framework de formulaires PHP autonome. Il ne dépend d'aucun framework applicatif externe et fournit un noyau complet pour construire, afficher, soumettre, valider et mapper des formulaires simples ou complexes.
- architecture standalone, sans dépendance à Symfony, Laravel ou autre framework
- API builder fluide via
Application\FormGenerator - API factory / types de formulaires via
Application\FormFactory FormTypeInterfaceréutilisable- formulaires imbriqués
CollectionTyperécursiffieldsetnatifs, y compris imbriqués- mapping tableau et mapping objet
- validation par contraintes
- cycle d'événements :
form.pre_set_dataform.pre_submitform.submitform.post_submitform.validation_error
- renderer HTML standalone
- thèmes HTML :
DefaultThemeBootstrap5ThemeTailwindTheme
- export JSON schema
- CSRF via abstraction dédiée
- gestion automatique du
multipart/form-datadès qu'un champ fichier est ajouté - support des types de champs historiques du legacy
- captcha alphanumérique sensible à la casse avec validation serveur
TextTypeTextareaTypeEditorTypeEmailTypePasswordTypeSearchTypePhoneTypeUrlTypeHiddenType
DateTypeDatetimeTypeDatetimeLocalTypeTimeTypeMonthTypeWeekType
IntegerTypeFloatTypeNumberTypeRangeTypeColorType
CheckboxTypeRadioTypeSelectTypeYesNoTypeCountryTypeDatalistType
FileTypeAudioTypeImageTypeVideoType
ButtonTypeSubmitTypeResetType
CollectionTypeCaptchaType
use Iriven\PhpFormGenerator\Application\FormGenerator;
use Iriven\PhpFormGenerator\Presentation\Html\HtmlRenderer;
$generator = (new FormGenerator('contact'))
->open([
'method' => 'POST',
'action' => '/contact',
'csrf_protection' => true,
])
->addFieldset([
'legend' => 'Contact',
'description' => 'Informations principales',
])
->addText('name', ['label' => 'Nom', 'required' => true])
->addEmail('email', ['label' => 'Email', 'required' => true])
->addTextarea('message', ['label' => 'Message'])
->addCaptcha('captcha', ['label' => 'Code'])
->endFieldset()
->addSubmit('send', ['label' => 'Envoyer']);
$form = $generator->getForm();
echo (new HtmlRenderer())->renderForm($form->createView());Méthodes disponibles sur FormGenerator :
addText()addEmail()addTextarea()addEditor()addCheckbox()addHidden()addSubmit()addButton()addReset()addFile()addAudio()addImage()addVideo()addCountries()addCountry()addDatetime()addDatetimeLocal()addDate()addTime()addMonth()addWeek()addInteger()addFloat()addNumber()addRange()addColor()addPassword()addPhone()addSearch()addUrl()addRadio()addSelect()addYesNo()addDatalist()addCaptcha()addCollection()addFieldset()endFieldset()
use Iriven\PhpFormGenerator\Application\FormFactory;
use Iriven\PhpFormGenerator\Domain\Contract\FormTypeInterface;
use Iriven\PhpFormGenerator\Domain\Contract\FormBuilderInterface;
use Iriven\PhpFormGenerator\Infrastructure\Http\ArrayRequest;
use Iriven\PhpFormGenerator\Infrastructure\Options\OptionsResolver;
final class ContactType implements FormTypeInterface
{
public function buildForm($builder, array $options = []): void
{
$builder
->add('name', \Iriven\PhpFormGenerator\Domain\Field\TextType::class, ['required' => true])
->add('email', \Iriven\PhpFormGenerator\Domain\Field\EmailType::class, ['required' => true])
->add('captcha', \Iriven\PhpFormGenerator\Domain\Field\CaptchaType::class, ['label' => 'Security code'])
->add('submit', \Iriven\PhpFormGenerator\Domain\Field\SubmitType::class, ['label' => 'Send']);
}
public function configureOptions($resolver): void
{
$resolver->setDefaults([
'method' => 'POST',
'csrf_protection' => true,
]);
}
}
$factory = new FormFactory();
$form = $factory->create(ContactType::class);
$request = new ArrayRequest('POST', [
'form' => [
'name' => 'Jane',
'email' => 'jane@example.com',
'captcha' => 'Ab12X',
'_token' => '...',
],
]);
$form->handleRequest($request);Pour les cas basiques, utilise FormGenerator.
Pour factoriser, implémente un FormTypeInterface et crée le formulaire via FormFactory.
Un champ peut être un sous-formulaire si son type implémente FormTypeInterface.
CollectionType permet de gérer une liste d'entrées homogènes, par exemple des lignes de facture.
Le framework sait mapper vers un tableau ou vers un objet métier.
Tu peux ajouter des listeners et subscribers sur le dispatcher interne.
Dès qu'un champ FileType, AudioType, ImageType ou VideoType est ajouté, le formulaire est automatiquement rendu avec enctype="multipart/form-data".
Le CaptchaType :
- génère automatiquement un code alphanumérique
- longueur configurable de 5 à 8 caractères
- est sensible à la casse
- valide côté serveur via le
SessionCaptchaManager - affiche un challenge SVG sans dépendance GD
Exemple :
$generator->addCaptcha('captcha', [
'label' => 'Code de sécurité',
'min_length' => 5,
'max_length' => 8,
]);use Iriven\PhpFormGenerator\Presentation\Html\HtmlRenderer;
use Iriven\PhpFormGenerator\Presentation\Html\Theme\Bootstrap5Theme;
echo (new HtmlRenderer(new Bootstrap5Theme()))->renderForm($form->createView());Les contraintes intégrées incluent notamment :
RequiredEmailLengthChoiceRegexUrlMinMaxRangeCountCallbackFileMimeTypeMaxFileSize
- les noms canoniques sont
addDatetime()etaddTextarea() multipart/form-dataest géré automatiquement- le projet est autonome et ne repose sur aucun framework externe
The project now ships with reusable application-level form types under src/Application/FormType.
InvoiceType demonstrates a realistic business form with:
- nested
CustomerType DatetimeTypeinvoice date fieldCollectionTypeofInvoiceLineType- fieldset grouping
- submit button
Typical usage:
use Iriven\PhpFormGenerator\Application\FormFactory;
use Iriven\PhpFormGenerator\Application\FormType\InvoiceType;
$form = (new FormFactory())->create(InvoiceType::class, [], [
'name' => 'invoice',
]);RegistrationType demonstrates a secure registration workflow with:
- email field with
RequiredandEmail - password and confirmation fields
- terms checkbox with server-side validation
- built-in alphanumeric captcha
- form-level password confirmation validation
Typical usage:
use Iriven\PhpFormGenerator\Application\FormFactory;
use Iriven\PhpFormGenerator\Application\FormType\RegistrationType;
$form = (new FormFactory())->create(RegistrationType::class, [], [
'name' => 'registration',
]);