https://github.com/bramstroker/zf2-form
ZF2 module for extending forms with live clientside validation without need to write js validation code
https://github.com/bramstroker/zf2-form
Last synced: 10 months ago
JSON representation
ZF2 module for extending forms with live clientside validation without need to write js validation code
- Host: GitHub
- URL: https://github.com/bramstroker/zf2-form
- Owner: bramstroker
- Created: 2012-11-23T10:56:08.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2020-11-13T17:52:41.000Z (over 5 years ago)
- Last Synced: 2024-12-10T02:20:27.263Z (over 1 year ago)
- Language: PHP
- Size: 327 KB
- Stars: 39
- Watchers: 7
- Forks: 23
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# StrokerForm
[](https://travis-ci.org/bramstroker/zf2-form)
[](https://coveralls.io/r/bramstroker/zf2-form)
[](https://scrutinizer-ci.com/g/bramstroker/zf2-form/?branch=master)
[](https://insight.sensiolabs.com/projects/ee332161-bb3b-401b-bbce-f8572558e998)
[](https://packagist.org/packages/stroker/form)
[](http://hhvm.h4cc.de/package/stroker/form)
ZF2 module for extending forms with live clientside validation without need to write js validation code.
You only need to define your validation rules server side with ZF2 and this module automaticaly adds the same rules with [jQueryValidate](http://docs.jquery.com/Plugins/Validation).
In case a client side version of the validation rule doesn't exist a fallback is done using ajax.
For basic usage examples see the sandbox project [StrokerFormSandbox](https://github.com/bramstroker/zf2-form-sandbox).
## BC Breaks since 1.0.0
- Support for PHP versions lower than 5.6 is dropped.
- `StrokerForm\Renderer\JqueryValidate\Rule\RuleInterface` has been extended with a new method `canHandle`. This method is called on a rule to check if it can process a certain validator. When you have some custom rules you'll need to update them to implement this new method.
## BC Breaks since 0.1.0
For the new version you need to copy `config/strokerform.global.php.dist` to your projects `config/autoload` dir.
## Installation
Installation of StrokerForm uses composer. For composer documentation, please refer to
[getcomposer.org](http://getcomposer.org/).
1. `cd my/project/directory`
2. create or modify the `composer.json` file within your ZF2 application file with
following contents:
```json
{
"require": {
"stroker/form": "*"
}
}
```
3. install composer via `curl -s https://getcomposer.org/installer | php` (on windows, download
https://getcomposer.org/installer and execute it with PHP). Then run `php composer.phar install`
4. open `my/project/directory/configs/application.config.php` and add the following key to your `modules`:
```php
'StrokerForm',
```
6. copy the file `config/strokerform.global.php.dist` from `vendor\stroker\zf2-form` to your projects `config/autoload` directory and rename it to `strokerform.global.php`.
5. copy the assets to your public folder (my/project/directory/public).
## Usage
First we need to make sure jquery is loaded by our application and the headScript() and inlineScript() view helpers are called. If you already have this in place you can skip this step.
```html
headLink() ?>
headScript()->prependFile('//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js') ?>
content; ?>
inlineScript() ?>
```
For the ajax validation to work inputfilters needs to be hooked to the form.
We need to create a serviceFactory and register it with a unique alias to the formManager (this is an pluginManager).
If the inputFilters are already set to the form (i.e. in your form constructor) it's enough to register the form as an invokable
```php
setInputFilter($model->getInputFilter());
return $form;
}
}
```
Now let's add our new factory to the formManager.
```php
array(
'forms' => array(
'factories' => array(
'my_form_alias' => 'MyProject\Service\MyFormFactory'
)
)
)
);
```
Last thing we need to do is invoking the StrokerFormPrepare view helper where you are rendering your form.
This view helper add all the needed javascripts to the headScript view helper
```php
strokerFormPrepare('my_form_alias');
// Do your normal form rendering here
```
## Renderers
A renderer should implement the RendererInterface and is responsible for modifying the form rendering (setting inline javascript, modifying the form element attributes, view helpers etc.).
Currently only the jqueryValidate renderer is available. Support for other validation libraries can be implemented as a seperate renderer.
### JqueryValidate
#### Options
- `include_assets`: Whether you want the view helper to include the needed assets or you like to do it yourself using a asset manager
- `use_twitter_bootstrap`: Set this to true if you are using twitter bootstrap.
- `validate_options`: Options for the jquery validate plugin. See [jqueryValidate options](http://docs.jquery.com/Plugins/Validation/validate#toptions) for all possible options. i.e. if you also want to validate on keypress you can set onkeyup to true.
- `disable_ajax_fallback`: Disables AJAX fallback for non available client side validators
#### Styling
If you are using twitter bootstrap and the recommended form structure the styling works out of the box.
When you are using the ZF2 view helpers for your form you could style the input fields `error` and `valid` classes which are added on the fly by the jquery plugin.
## Excluding elements from clientside validation
You can set the option `strokerform-exclude` on a form element
```php
$name = new Element('name');
$name->setLabel('Your name');
$name->setOption('strokerform-exclude', true);
```