Skip to content

Commit d553202

Browse files
Update documentation to reflect Attribute usage possibility (#51)
* Add attribute support, fix uppercase issue with getter * Update documentation to mention Attribute usage * Update README.md * Update README.md --------- Co-authored-by: Mrtn Schndlr <mrtnschndlr@gmail.com>
1 parent b329fa2 commit d553202

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

README.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,23 @@ $ composer require barbieswimcrew/zip-code-validator
2121
## What now?
2222
For validating a zip code you need to instantiate a new ZipCode class provided by this package to set it as a constraint to your form field, for example:
2323

24-
```bash
25-
24+
```php
2625
<?php
2726
//...
2827
$form = $this->createFormBuilder($address)
29-
->add('zipcode', TextType::class, [
30-
'constraints' => array(
31-
new ZipCodeValidator\Constraints\ZipCode([
32-
'iso' => 'DE'
33-
])
34-
]
35-
))
36-
->add('save', SubmitType::class, ['label' => 'Create Task'])
37-
->getForm();
28+
->add('zipcode', TextType::class, [
29+
'constraints' => [
30+
new ZipCodeValidator\Constraints\ZipCode([
31+
'iso' => 'DE'
32+
])
33+
]
34+
])
35+
->add('save', SubmitType::class, ['label' => 'Create Task'])
36+
->getForm();
3837
```
3938

4039
Another way would be to use the constraint as an annotation of a class property, for example:
41-
```bash
40+
```php
4241
<?php
4342

4443
use ZipCodeValidator\Constraints\ZipCode;
@@ -52,6 +51,19 @@ class Address
5251
}
5352
```
5453

54+
You can also use it as a PHP8 Attribute, with parameters passed as an array of options, for example:
55+
```php
56+
<?php
57+
58+
use ZipCodeValidator\Constraints\ZipCode;
59+
60+
class Address
61+
{
62+
#[ZipCode(['iso'=>'DE'])
63+
protected $zipCode;
64+
}
65+
```
66+
5567
> Please consider to inject a valid ISO 3166 2-letter country code (e.g. DE, US, FR)!
5668
5769
> NOTE: This library validates against known zip code regex patterns and does not validate the existence of a zipcode.

0 commit comments

Comments
 (0)