Skip to content

Commit f1aa771

Browse files
committed
Wire up Symfony Forms, Twig extension and add initial docs
1 parent 1b79731 commit f1aa771

File tree

6 files changed

+149
-14
lines changed

6 files changed

+149
-14
lines changed

README.md

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
# This is my package accessible-forms
1+
# Accessible forms
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/studio24/accessible-forms.svg?style=flat-square)](https://packagist.org/packages/studio24/accessible-forms)
44
[![Tests](https://img.shields.io/github/actions/workflow/status/studio24/accessible-forms/php.yml?branch=main&label=tests&style=flat-square)](https://github.com/studio24/accessible-forms/actions/workflows/php.yml)
55

6-
This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.
6+
Accessible forms in Laravel and Symfony.
7+
8+
## Todo
9+
10+
Add any notes here on problems to solve:
11+
12+
- What helpers do we need to make form generation easier?
13+
- Twig in Barryvdh\Form appears to be installed separately to TwigBridge - is this an issue? Twig extension appears to be available for both.
14+
- Can we automate adding the template paths, currently copy config/form.php across
715

816
## Requirements
917

@@ -17,9 +25,74 @@ You can install the package via [Composer](https://getcomposer.org/):
1725
composer require studio24/accessible-forms
1826
```
1927

28+
### Local install
29+
During testing you can install this from a local copy via:
30+
31+
```shell
32+
composer config repositories.local path "~/Sites/studio24/accessible-forms/"
33+
composer require studio24/accessible-forms:dev-main
34+
```
35+
36+
To remove this after testing:
37+
38+
```shell
39+
ddev composer config repositories.local --unset
40+
ddev composer update
41+
```
42+
43+
See https://github.com/studio24/dev-playbook/blob/main/composer/testing-local-packages.md
44+
2045
## Usage
2146

22-
Include some short usage instructions here. If you need more extensive docs save these in docs/ and link to them from here.
47+
### Laravel
48+
49+
Add the service provider to `bootstrap/providers.php`
50+
51+
```php
52+
return [
53+
Barryvdh\Form\ServiceProvider::class,
54+
];
55+
```
56+
57+
To enable TwigExtension in your normal Twig templates (not form twig templates at present) edit `config/twibridge.php`
58+
59+
```php
60+
return [
61+
'extensions' => [
62+
'enabled' => [
63+
'Studio24\AccessibleForms\Twig\AccessibleFormsExtension',
64+
],
65+
],
66+
];
67+
```
68+
69+
Create a form class:
70+
71+
See https://symfony.com/doc/current/forms.html#creating-form-classes
72+
73+
The form processing workflow is:
74+
75+
1. Display form
76+
2. Form submitted via POST request
77+
3. Form request data is validated
78+
4. If pass validation, do something, and redirect to success page
79+
5. If fail validation, redisplay form with validation messages (and no redirect)
80+
81+
Add form processing code to your controller:
82+
83+
```php
84+
$form = $this->createForm(ContactForm::class);
85+
86+
$form->handleRequest($request);
87+
88+
if ($form->isSubmitted() && $form->isValid()) {
89+
// Do something, e.g. save data
90+
return 'Form submitted successfully';
91+
}
92+
93+
// Display form to user, with validation if submitted and fails validation
94+
return view('template-name', ['form' => $form->createView()]);
95+
```
2396

2497
## Testing
2598

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
"name": "studio24/accessible-forms",
33
"description": "This is my package accessible-forms",
44
"keywords": [
5-
"studio24"
5+
"studio24",
6+
"forms",
7+
"accessibility",
8+
"laravel",
9+
"symfony"
610
],
711
"type": "library",
812
"license": "MIT",
913
"homepage": "https://github.com/studio24/accessible-forms",
1014
"require": {
11-
"php": "^8.2"
15+
"php": "^8.2",
16+
"barryvdh/laravel-form-bridge": "^0.7.4"
1217
},
1318
"require-dev": {
1419
"phpstan/phpstan": "^2.1",
@@ -27,6 +32,9 @@
2732
}
2833
},
2934
"scripts": {
35+
"post-install-cmd": [
36+
"@php -r \"file_exists('config/form.php') || copy('vendor/studio24/accessible-forms/config/form.php', 'config/form.php');\""
37+
],
3038
"test": "./vendor/bin/phpunit",
3139
"cs": "./vendor/bin/phpcs",
3240
"format": "./vendor/bin/phpcbf",

config/form.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Form theme
8+
|--------------------------------------------------------------------------
9+
|
10+
| Set the template to render the form.
11+
|
12+
| See https://symfony.com/doc/current/form/form_themes.html
13+
*/
14+
'theme' => 'accessible-forms.html.twig',
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Form template directories
19+
|--------------------------------------------------------------------------
20+
|
21+
| Add custom template directories to render the form.
22+
|
23+
| See http://symfony.com/doc/current/form/form_customization.html
24+
*/
25+
'template_directories' => [
26+
__DIR__ . '/../vendor/studio24/accessible-forms/src/Resources/views/form'
27+
],
28+
29+
'defaults' => [
30+
'required' => true,
31+
]
32+
];

src/AccessibleForms.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{# Extends vendor/symfony/twig-bridge/Resources/views/Form/form_div_layout.html.twig #}
2+
{% use 'form_div_layout.html.twig' %}
3+
4+
{%- block form_label_content -%}
5+
{%- set name = "A11Y " ~ name -%}
6+
{{- parent() -}}
7+
{%- endblock form_label_content -%}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Studio24\AccessibleForms\Twig;
5+
6+
use Twig\Extension\AbstractExtension;
7+
use Twig\TwigFilter;
8+
9+
class AccessibleFormsExtension extends AbstractExtension
10+
{
11+
public function getFilters()
12+
{
13+
return [
14+
new TwigFilter('validation_message', [$this, 'validationMessage']),
15+
];
16+
}
17+
18+
public function validationMessage(string $title): string
19+
{
20+
// @todo process this message based on form validation
21+
return "There are X problems with your request - " . $title;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)