Skip to content

Commit

Permalink
Data binding (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyexeter authored May 8, 2023
1 parent cb2930f commit 0c2a1ca
Show file tree
Hide file tree
Showing 37 changed files with 2,870 additions and 296 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ jobs:
- name: Psalm - Static Analysis
run: php vendor/bin/psalm --no-cache --show-info=false --stats --output-format=github --threads=$(nproc)
if: matrix.php-versions == 8.1

- name: PHPUnit - Tests
run: php vendor/bin/phpunit -v --do-not-cache-result
if: matrix.php-versions == 8.1
2 changes: 2 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
$config = new Config();

$config
->setCacheFile(__DIR__ . '/var/.php-cs-fixer.cache')
->getFinder()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->append([__FILE__])
;

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2019-2022 Andy Palmer
Copyright (c) 2019-2023 Andy Palmer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
124 changes: 2 additions & 122 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,129 +62,9 @@ if ($form->isSubmitted() && $form->isValid()) {

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

## Rendering Individual Fields
## Documentation

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:

```html
<div class="container">
<?= $form->renderStart(); ?>
<div class="row">
<div class="col-6">
<?= $form->renderField('first_name'); ?>
</div>
<div class="col-6">
<?= $form->renderField('last_name'); ?>
</div>
</div>
<?= $form->renderEnd(); ?>
</div>
```

By default, `renderEnd` will render all remaining unrendered fields before rendering the closing </form> tag. To prevent this, pass `false` as the first argument:

```php
<?= $form->renderEnd(false); ?>
```

## Collections

The `CollectionType` can be used to add/remove multiple entries of the same field or set of fields:

```php
use Palmtree\Form\FormBuilder;
$builder = (new FormBuilder('collection_example'))
->add('name', 'collection', [
'entry_type' => 'text',
'classes' => ['names-collection']
])
->add('submit', 'submit');
```

```html
<script src="/path/to/palmtree-form.pkgd.js"></script>
<script>
$(function () {
$('.names-collection').palmtreeFormCollection({
minEntries: 1,
maxEntries: 4,
labels: {
add: 'Add person',
remove: 'Remove person'
}
});
});
</script>
```

See the [collection example](examples/collection) for a more advanced use-case.

## Constraints

Constraints allow you to validate a field type. The current built in constraints are:

| Constraint | Description |
|-----------------------------------------|-----------------------------------------------------------------------------------|
| [NotBlank](src/Constraint/NotBlank.php) | Ensures the field is not empty. Allows values of '0' |
| [Email](src/Constraint/Email.php) | Ensures the field is a valid email address |
| [Number](src/Constraint/Number.php) | Ensures the field is numeric and optionally between a range |
| [Length](src/Constraint/Length.php) | Ensures the field has a minimum and/or maximum length of characters |
| [Matching](src/Constraint/Matching.php) | Ensures the field matches another fields value. Useful for password confirmations |

By default, all required fields have a NotBlank constraint.
Email fields have an email constraint and number fields a Number constraint.

## Using Constraints
```php
// Add an age field where the value must be between 18 and 80
$builder->add('age', 'number', [
'constraints' => [
new Constraint\Number(['min' => 18, 'max' => 80])
]
]);

// Add a password and confirm password field with a minimum length of 8 characters
$builder->add('password', 'repeated', [
'repeatable_type' => 'password',
'constraints' => [
new Constraint\Length(['min' => 8])
]
]);

```

You can also implement your own constraints, they just need to implement the [ConstraintInterface](src/Constraint/ConstraintInterface.php)

## File Uploads

### UploadedFile Object

When you retrieve a FileType's data from a form, an instance of [UploadedFile](src/UploadedFile.php) will be returned.
This is a small wrapper object around PHP's native uploaded file array.

### File Constraints

The following constraints can be used on the FileType field:

| Constraint | Description |
|------------------------------------------------|---------------------------------------------------|
| [Extension](src/Constraint/File/Extension.php) | Ensures the file has an allowed extension |
| [MimeType](src/Constraint/File/MimeType.php) | Ensures the file has an allowed mime type |
| [Size](src/Constraint/File/MimeType.php) | Ensures the file size is between an allowed range |

See the [file upload example](examples/fileupload/index.php) for usage examples of these constraints

## Shorthand Type Values

Shorthand values for [built-in types](src/Type) are determined by lower-casing the class name and removing the "Type" suffix.
For example:

| Class | Shorthand Value |
|----------------|-----------------|
| TextType | text |
| NumberType | number |
| EmailType | email |
| CollectionType | collection |
[View the documentation](docs/index.md) for more advanced usage.

## Examples

Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"Palmtree\\Form\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Palmtree\\Form\\Examples\\": "examples",
"Palmtree\\Form\\Test\\": "tests"
}
},
"require": {
"php": ">=7.1",
"palmtree/argparser": "^2.1",
Expand All @@ -23,7 +29,8 @@
},
"require-dev": {
"palmtree/php-cs-fixer-config": "^2.0",
"vimeo/psalm": "^4.18"
"vimeo/psalm": "^4.18",
"phpunit/phpunit": "^9.6"
},
"suggest": {
"ext-curl": "For Google Recaptcha support",
Expand All @@ -45,6 +52,7 @@
},
"scripts": {
"psalm": "@php vendor/bin/psalm --no-cache",
"fix": "@php vendor/bin/php-cs-fixer fix"
"fix": "@php vendor/bin/php-cs-fixer fix",
"test": "@php vendor/bin/phpunit"
}
}
Loading

0 comments on commit 0c2a1ca

Please sign in to comment.