Skip to content

Commit 0c2a1ca

Browse files
authored
Data binding (#6)
1 parent cb2930f commit 0c2a1ca

37 files changed

+2870
-296
lines changed

.github/workflows/build.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,7 @@ jobs:
4949
- name: Psalm - Static Analysis
5050
run: php vendor/bin/psalm --no-cache --show-info=false --stats --output-format=github --threads=$(nproc)
5151
if: matrix.php-versions == 8.1
52+
53+
- name: PHPUnit - Tests
54+
run: php vendor/bin/phpunit -v --do-not-cache-result
55+
if: matrix.php-versions == 8.1

.php-cs-fixer.dist.php

+2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
$config = new Config();
88

99
$config
10+
->setCacheFile(__DIR__ . '/var/.php-cs-fixer.cache')
1011
->getFinder()
1112
->in(__DIR__ . '/src')
13+
->in(__DIR__ . '/tests')
1214
->append([__FILE__])
1315
;
1416

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2019-2022 Andy Palmer
1+
Copyright (c) 2019-2023 Andy Palmer
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

+2-122
Original file line numberDiff line numberDiff line change
@@ -62,129 +62,9 @@ if ($form->isSubmitted() && $form->isValid()) {
6262

6363
See the [examples](examples) directory for examples using AJAX, file uploads, collections and more.
6464

65-
## Rendering Individual Fields
65+
## Documentation
6666

67-
Use the `renderStart`, `renderEnd`, `renderField` and `renderRest` methods for more fine-grained control over how fields are rendered, such as using Bootstrap's grid system:
68-
69-
```html
70-
<div class="container">
71-
<?= $form->renderStart(); ?>
72-
<div class="row">
73-
<div class="col-6">
74-
<?= $form->renderField('first_name'); ?>
75-
</div>
76-
<div class="col-6">
77-
<?= $form->renderField('last_name'); ?>
78-
</div>
79-
</div>
80-
<?= $form->renderEnd(); ?>
81-
</div>
82-
```
83-
84-
By default, `renderEnd` will render all remaining unrendered fields before rendering the closing </form> tag. To prevent this, pass `false` as the first argument:
85-
86-
```php
87-
<?= $form->renderEnd(false); ?>
88-
```
89-
90-
## Collections
91-
92-
The `CollectionType` can be used to add/remove multiple entries of the same field or set of fields:
93-
94-
```php
95-
use Palmtree\Form\FormBuilder;
96-
$builder = (new FormBuilder('collection_example'))
97-
->add('name', 'collection', [
98-
'entry_type' => 'text',
99-
'classes' => ['names-collection']
100-
])
101-
->add('submit', 'submit');
102-
```
103-
104-
```html
105-
<script src="/path/to/palmtree-form.pkgd.js"></script>
106-
<script>
107-
$(function () {
108-
$('.names-collection').palmtreeFormCollection({
109-
minEntries: 1,
110-
maxEntries: 4,
111-
labels: {
112-
add: 'Add person',
113-
remove: 'Remove person'
114-
}
115-
});
116-
});
117-
</script>
118-
```
119-
120-
See the [collection example](examples/collection) for a more advanced use-case.
121-
122-
## Constraints
123-
124-
Constraints allow you to validate a field type. The current built in constraints are:
125-
126-
| Constraint | Description |
127-
|-----------------------------------------|-----------------------------------------------------------------------------------|
128-
| [NotBlank](src/Constraint/NotBlank.php) | Ensures the field is not empty. Allows values of '0' |
129-
| [Email](src/Constraint/Email.php) | Ensures the field is a valid email address |
130-
| [Number](src/Constraint/Number.php) | Ensures the field is numeric and optionally between a range |
131-
| [Length](src/Constraint/Length.php) | Ensures the field has a minimum and/or maximum length of characters |
132-
| [Matching](src/Constraint/Matching.php) | Ensures the field matches another fields value. Useful for password confirmations |
133-
134-
By default, all required fields have a NotBlank constraint.
135-
Email fields have an email constraint and number fields a Number constraint.
136-
137-
## Using Constraints
138-
```php
139-
// Add an age field where the value must be between 18 and 80
140-
$builder->add('age', 'number', [
141-
'constraints' => [
142-
new Constraint\Number(['min' => 18, 'max' => 80])
143-
]
144-
]);
145-
146-
// Add a password and confirm password field with a minimum length of 8 characters
147-
$builder->add('password', 'repeated', [
148-
'repeatable_type' => 'password',
149-
'constraints' => [
150-
new Constraint\Length(['min' => 8])
151-
]
152-
]);
153-
154-
```
155-
156-
You can also implement your own constraints, they just need to implement the [ConstraintInterface](src/Constraint/ConstraintInterface.php)
157-
158-
## File Uploads
159-
160-
### UploadedFile Object
161-
162-
When you retrieve a FileType's data from a form, an instance of [UploadedFile](src/UploadedFile.php) will be returned.
163-
This is a small wrapper object around PHP's native uploaded file array.
164-
165-
### File Constraints
166-
167-
The following constraints can be used on the FileType field:
168-
169-
| Constraint | Description |
170-
|------------------------------------------------|---------------------------------------------------|
171-
| [Extension](src/Constraint/File/Extension.php) | Ensures the file has an allowed extension |
172-
| [MimeType](src/Constraint/File/MimeType.php) | Ensures the file has an allowed mime type |
173-
| [Size](src/Constraint/File/MimeType.php) | Ensures the file size is between an allowed range |
174-
175-
See the [file upload example](examples/fileupload/index.php) for usage examples of these constraints
176-
177-
## Shorthand Type Values
178-
179-
Shorthand values for [built-in types](src/Type) are determined by lower-casing the class name and removing the "Type" suffix.
180-
For example:
181-
182-
| Class | Shorthand Value |
183-
|----------------|-----------------|
184-
| TextType | text |
185-
| NumberType | number |
186-
| EmailType | email |
187-
| CollectionType | collection |
67+
[View the documentation](docs/index.md) for more advanced usage.
18868

18969
## Examples
19070

composer.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
"Palmtree\\Form\\": "src"
1616
}
1717
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Palmtree\\Form\\Examples\\": "examples",
21+
"Palmtree\\Form\\Test\\": "tests"
22+
}
23+
},
1824
"require": {
1925
"php": ">=7.1",
2026
"palmtree/argparser": "^2.1",
@@ -23,7 +29,8 @@
2329
},
2430
"require-dev": {
2531
"palmtree/php-cs-fixer-config": "^2.0",
26-
"vimeo/psalm": "^4.18"
32+
"vimeo/psalm": "^4.18",
33+
"phpunit/phpunit": "^9.6"
2734
},
2835
"suggest": {
2936
"ext-curl": "For Google Recaptcha support",
@@ -45,6 +52,7 @@
4552
},
4653
"scripts": {
4754
"psalm": "@php vendor/bin/psalm --no-cache",
48-
"fix": "@php vendor/bin/php-cs-fixer fix"
55+
"fix": "@php vendor/bin/php-cs-fixer fix",
56+
"test": "@php vendor/bin/phpunit"
4957
}
5058
}

0 commit comments

Comments
 (0)