Skip to content

Commit e6560c0

Browse files
committed
Much needed documentation
1 parent 0dad0e1 commit e6560c0

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,155 @@
11
# parsley-bundle
22
A Symfony bundle to help wire Parsley.js into projects
3+
4+
## Introduction
5+
6+
Wouldn't it be nice if there was a way to expose [Symfony Form Constraints](https://symfony.com/doc/current/validation.html#constraints) and Entity Annotations to
7+
the user as client side validations? Yes. Yes it would. It'd also be nice to stand on the shoulders of giants an use a well respected client side validation library
8+
to do this! To that end, I started (about 2 years before typing this sentence) to think about using [Parsley.js](http://parsleyjs.org/) to do the validation by converting
9+
Symfony validations into parsley ``data-parsley-*`` attributes.
10+
11+
So, eventually, I wrote something.
12+
13+
## Installation
14+
15+
You know the score:
16+
17+
```bash
18+
composer require c0ntax/parsley-bundle
19+
```
20+
21+
And don't forget to add the following to your Kernel if you're not using Flex:
22+
23+
```php
24+
public function registerBundles()
25+
{
26+
$bundles = [
27+
// ...
28+
new C0ntax\ParsleyBundle\C0ntaxParsleyBundle(),
29+
// ...
30+
];
31+
}
32+
```
33+
34+
## Configuration
35+
36+
Currently, a little configuration-light, you can add the following:
37+
38+
```yaml
39+
c0ntax_parsley:
40+
enabled: true # Obviously set to false to switch it all off
41+
field:
42+
trigger: focusout # Set all fields to trigger validation on one or more jQuery events
43+
```
44+
45+
## Supported Parsley Validations
46+
47+
Since this library is very much alpha, I haven't had time to add all the [validations](http://parsleyjs.org/doc/index.html#validators) in yet, so here's a list of ones that are currently supported
48+
49+
* Email
50+
* Length
51+
* MaxLength
52+
* MinLength
53+
* Pattern
54+
55+
## Usage
56+
57+
### Client Side Only Validations
58+
59+
For reasons that I can't quite imagine, you might only want to just add client side validation. To do this, simply add a parsley constraint to your form:
60+
61+
```php
62+
public function buildForm(FormBuilderInterface $builder, array $options)
63+
{
64+
$builder
65+
->add(
66+
'textThing',
67+
TextType::class,
68+
[
69+
'parsleys' => [
70+
new \C0ntax\ParsleyBundle\Constraint\MinLength(2, 'You need more than %s chars'),
71+
],
72+
]
73+
)
74+
;
75+
}
76+
```
77+
78+
The above example will add a client side validation to ensure that the data is greater than or equal to 2 characters
79+
80+
You can add any number of these 'parsleys'
81+
82+
### Form and Client Side Validation
83+
84+
This use-case makes a little more sense. This is for where you want to explicitly add constraints to a form and have them validate on both the client and the server (just in case the user has they javascript turned off or is messing about with your forms)
85+
86+
```php
87+
public function buildForm(FormBuilderInterface $builder, array $options)
88+
{
89+
$builder
90+
->add(
91+
'textThing',
92+
TextType::class,
93+
[
94+
'constrains' => [
95+
new \Symfony\Component\Validator\Constraints\Length(['min' => 2, 'message' => 'You need more that {{ limit }} chars']),
96+
]
97+
]
98+
)
99+
;
100+
}
101+
```
102+
103+
If you've used Symfony Forms before, you'll probably notice that there's nothing special here! What happens behind the scenes is that this library picks up Symfony constraints applied to form elements and converts them to their corresponding Parsley constraints.
104+
105+
So, as long as there is a mapping (See [Supported Parsley Validations](#Supported Parsley Validations)) for the Symfony constraint, it'll be added to the form automagically.
106+
107+
### Entity Annotation and Client Side Validation
108+
109+
One of the really cool things about Symfony is that you can 'configure' the validations for an Entity within the entity itself. (Some people will tell you that this is a bad thing. They are wrong in the face.)
110+
111+
These are also picked up by the library, so added the 'assert' to the Entity will 'just work'(tm).
112+
113+
```php
114+
class Entity {
115+
/**
116+
* @Assert\Length(min=2)
117+
*/
118+
private $textThing;
119+
}
120+
```
121+
122+
### 3rd-Party Entity Annotation and Client Side Validation
123+
124+
This is where I really wanted to get to with this library. Imagine the crazy world of the future where you have a nicely swagger/OASed spec for your API. You then use [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) to produce a portable entity model.
125+
You may even tweak Codegen so that it uses Symfony Annotations for its data validation. Well, out of the box, those annotations will now be converted to Client Side Validations too! YAY! But... What swagger/OAS doesn't account for is the error message that you might want to display
126+
to the user. That's where the ErrorMessage Directive comes it. With this, you can add in your own error messages for annotations that you might not have 'control' over.
127+
128+
Let's assume that the Entity in the example above is out of your control. It's come in via a 3rd party library. You may want to give it a specific error message.
129+
130+
```php
131+
public function buildForm(FormBuilderInterface $builder, array $options)
132+
{
133+
$builder
134+
->add(
135+
'textThing',
136+
TextType::class,
137+
[
138+
'parsleys' => [
139+
new \C0ntax\ParsleyBundle\Directive\ConstraintErrorMessage(\C0ntax\ParsleyBundle\Constraint\MinLength::class, 'You need more than %s chars'),
140+
],
141+
]
142+
)
143+
;
144+
}
145+
```
146+
147+
*NOTE* The class passed to identify where to attache the error message is the ParsleyBundle one and not the Symfony one!
148+
149+
## Rolling your own
150+
151+
You can add your own parsley directives by simply implementing the ``DirectiveInterface``. The only requirement is that it passes back an array of attributes that will be injected into your form HTML.
152+
153+
## Is that it
154+
155+
Yes, for now. As mentioned, this is very much 'alpha' as it only supports a small subset of the Symfony Validations, for now...

0 commit comments

Comments
 (0)