Skip to content

Commit

Permalink
Update documentation to reflect Attribute usage possibility (#51)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
Pixelshaped and barbieswimcrew authored May 2, 2023
1 parent b329fa2 commit d553202
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ $ composer require barbieswimcrew/zip-code-validator
## What now?
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:

```bash

```php
<?php
//...
$form = $this->createFormBuilder($address)
->add('zipcode', TextType::class, [
'constraints' => array(
new ZipCodeValidator\Constraints\ZipCode([
'iso' => 'DE'
])
]
))
->add('save', SubmitType::class, ['label' => 'Create Task'])
->getForm();
->add('zipcode', TextType::class, [
'constraints' => [
new ZipCodeValidator\Constraints\ZipCode([
'iso' => 'DE'
])
]
])
->add('save', SubmitType::class, ['label' => 'Create Task'])
->getForm();
```

Another way would be to use the constraint as an annotation of a class property, for example:
```bash
```php
<?php

use ZipCodeValidator\Constraints\ZipCode;
Expand All @@ -52,6 +51,19 @@ class Address
}
```

You can also use it as a PHP8 Attribute, with parameters passed as an array of options, for example:
```php
<?php

use ZipCodeValidator\Constraints\ZipCode;

class Address
{
#[ZipCode(['iso'=>'DE'])
protected $zipCode;
}
```

> Please consider to inject a valid ISO 3166 2-letter country code (e.g. DE, US, FR)!
> NOTE: This library validates against known zip code regex patterns and does not validate the existence of a zipcode.
Expand Down

0 comments on commit d553202

Please sign in to comment.