Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Build Symftony/form-handler
run-name: 🧑‍💻${{ github.actor }} Run build.
on: [ push ]
jobs:
Build:
name: PHP ${{ matrix.php-versions }} // composer ${{ matrix.composer-command }}
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.1', '8.2', '8.3' ]
composer-command:
- 'install'
- 'update --prefer-lowest'
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: xdebug

- name: "Composer -> Get cache directory"
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: "Composer -> Cache dependencies"
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: "Composer -> Install dependencies"
run: composer ${{ matrix.composer-command }} --no-progress --prefer-dist --optimize-autoloader

- name: "Composer -> Show"
run: composer show

- name: "Composer -> check platform requirements"
run: composer check-platform-reqs

# - name: "Rector -> Run"
# run: rector --dry-run

- name: "PHP cs fixer -> check"
run: vendor/bin/php-cs-fixer check src

- name: "PHPStan -> Run"
run: vendor/bin/phpstan analyse src

- name: "PHPUnit -> Run"
run: vendor/bin/phpunit --coverage-text
env:
XDEBUG_MODE: coverage
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
vendor
composer.lock

.phpunit.cache
.phpunit.result.cache

.php-cs-fixer.cache
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"symfony/form": "^6.1"
},
"require-dev": {
"phpunit/phpunit": "^9.1",
"phpunit/phpunit": "^10",
"symfony/dependency-injection": "^6.1",
"symfony/config": "^6.1",
"symfony/http-kernel": "^6.1",
Expand All @@ -23,7 +23,10 @@
"phpspec/prophecy-phpunit": "^2.1",
"symfony/translation": "^6.1",
"symfony/contracts": "^3.5@dev",
"symfony/validator": "^6.1"
"symfony/validator": "^6.1",
"phpstan/phpstan": "^1.11",
"friendsofphp/php-cs-fixer": "dev-master",
"rector/rector": "dev-main"
},
"suggest": {
"symfony/dependency-injection": "^6.1",
Expand Down
11 changes: 5 additions & 6 deletions examples/CustomFormHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Example;

use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -8,10 +10,7 @@

class CustomFormHandler extends FormHandler
{
/**
* @return \Symfony\Component\Form\FormInterface
*/
public function formChoice()
public function formChoice(): \Symfony\Component\Form\FormInterface
{
return $this->createForm(ChoiceType::class, 'my-value', null, [
'method' => 'GET',
Expand All @@ -30,7 +29,7 @@ public function formChoice()
]);
}

public function createFromRequest($request = null, $notSubmitted = true, $invalid = true)
public function createFromRequest(?mixed $request = null, $notSubmitted = true, $invalid = true)
{
$form = $this->createForm(ChoiceType::class, 'my-value', null, [
'method' => 'GET',
Expand All @@ -49,4 +48,4 @@ public function createFromRequest($request = null, $notSubmitted = true, $invali

return $this->handleRequest($form, $request);
}
}
}
30 changes: 20 additions & 10 deletions examples/custom-form-api.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

declare(strict_types=1);

namespace Example;

require_once __DIR__ . '/../vendor/autoload.php';
require_once 'CustomFormHandler.php';
require_once __DIR__ . '/CustomFormHandler.php';
use Symftony\FormHandler\Exception\FormException;
use Symftony\FormHandler\Form\Extension\Invalid\Type\InvalidTypeExtension;
use Symftony\FormHandler\Form\Extension\NotSubmitted\Type\NotSubmittedTypeExtension;
use Symftony\FormHandler\Form\Extension\TransformationFailed\Type\TransformationFailedTypeExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Validator\Validation;

// Initialize form component
// add not submitted type extension
// add invalid type extension
Expand All @@ -22,26 +23,26 @@
->addTypeExtension(new TransformationFailedTypeExtension())
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory();

// Initialize form handler
$formHandler = new CustomFormHandler();
$formHandler->setFormFactory($formFactory);

// Create the form
// Handle request
$exception = null;
try {
$result = $formHandler->createFromRequest();
} catch (FormException $e) {
$exception = $e;
} catch (FormException $formException) {
$exception = $formException;
}
?>
<html>
<head>
<title>Form handler throw NotSubmitted/Invalid Exception example</title>
</head>
<body>
<?php include 'menu.php'; ?>
<?php
include __DIR__ . '/menu.php';
?>
<div class="container">
<h1>Form handler will return default data when exception append</h1>
<div class="content">
Expand All @@ -67,13 +68,22 @@

<p class="important">/!\ When you use the handler to do some API you dont need the form in your controller. You just need your data or Exception /!\</p>
<div class="formdebug">
<code>$formHandler->handleRequest($form) : <?php if (isset($result)) var_export($result); ?>
<?php if (null !== $exception): ?>
<span class="error"><?= get_class($exception) . ' : ' . $exception->getMessage() ?></span>
<code>$formHandler->handleRequest($form) : <?php
if (isset($result)) {
var_export($result);
}

?>
<?php
if ($exception instanceof \Symftony\FormHandler\Exception\FormException): ?>
<span class="error"><?= $exception::class . ' : ' . $exception->getMessage() ?></span>
<?php endif ?>

?>
</code>
</div>
</div>
</div>
</body>
</html>
<?php
45 changes: 32 additions & 13 deletions examples/custom-form-web.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?php

declare(strict_types=1);

namespace Example;

require_once __DIR__ . '/../vendor/autoload.php';
require_once 'CustomFormHandler.php';
require_once __DIR__ . '/CustomFormHandler.php';
use Symftony\FormHandler\Exception\FormException;
use Symftony\FormHandler\Form\Extension\Invalid\Type\InvalidTypeExtension;
use Symftony\FormHandler\Form\Extension\NotSubmitted\Type\NotSubmittedTypeExtension;
use Symftony\FormHandler\Form\Extension\TransformationFailed\Type\TransformationFailedTypeExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\Validator\Validation;

// Initialize form component
// add not submitted type extension
// add invalid type extension
Expand All @@ -22,27 +23,27 @@
->addTypeExtension(new TransformationFailedTypeExtension())
->addExtension(new ValidatorExtension(Validation::createValidator()))
->getFormFactory();

// Initialize form handler
$formHandler = new CustomFormHandler();
$formHandler->setFormFactory($formFactory);

// Create the form
$form = $formHandler->formChoice();
// Handle request
$exception = null;
try {
$result = $formHandler->handleRequest($form);
} catch (FormException $e) {
$exception = $e;
} catch (FormException $formException) {
$exception = $formException;
}
?>
<html>
<head>
<title>Form handler throw NotSubmitted/Invalid Exception example</title>
</head>
<body>
<?php include 'menu.php'; ?>
<?php
include __DIR__ . '/menu.php';
?>
<div class="container">
<h1>Form handler will return default data when exception append</h1>
<div class="content">
Expand All @@ -67,23 +68,41 @@
</form>

<div class="formdebug">
<code>$form->isSubmitted() : <?= var_export($form->isSubmitted()) ?></code>
<code>$form->isValid() : <?= $form->isSubmitted() &&var_export($form->isValid()) ?></code>
<code>$form->isSubmitted() : <?php
<?= var_export($form->isSubmitted()) ?>
?></code>
<code>$form->isValid() : <?php
<?= $form->isSubmitted() &&var_export($form->isValid()) ?>
?></code>
<code>$form->getTransformationFailure() :
<?php if ($form->getTransformationFailure()): ?>
<?php
if ($form->getTransformationFailure()): ?>
<span class="warning"><?= $form->getTransformationFailure()->getMessage(); ?></span>
<?php else: ?>
null
<?php endif ?>

?>
</code>
<code>$form->getData() : <?= var_export($form->getData()); ?></code>
<code>$formHandler->handleRequest($form) : <?php if (isset($result)) var_export($result); ?>
<?php if (null !== $exception): ?>
<code>$form->getData() : <?php
<?= var_export($form->getData());
?></code>
<code>$formHandler->handleRequest($form) : <?php
if (isset($result)) {
var_export($result);
}

?>
<?php
if ($exception instanceof \Symftony\FormHandler\Exception\FormException): ?>
<span class="error"><?= get_class($exception) . ' : ' . $exception->getMessage() ?></span>
<?php endif ?>

?>
</code>
</div>
</div>
</div>
</body>
</html>
<?php
Loading