shopsys / jsformvalidator-bundle
Javascript validation for Symfony forms.
Installs: 69 928
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 57
Language:JavaScript
Type:symfony-bundle
Requires
- php: >=7.4
- symfony/form: ^4.4|^5.0
- symfony/validator: ^4.4|^5.0
Requires (Dev)
- ext-pdo_sqlite: *
- erusev/parsedown: ^1.6
- roave/security-advisories: dev-latest
- sensio/framework-extra-bundle: ^5.1
- symfony/asset: *
- symfony/console: *
- symfony/dotenv: *
- symfony/expression-language: *
- symfony/flex: ^1.17
- symfony/framework-bundle: *
- symfony/intl: *
- symfony/security-bundle: *
- symfony/translation: *
- symfony/twig-bundle: *
- symfony/webpack-encore-bundle: ^1.4
- symfony/yaml: *
- tgalopin/html-sanitizer-bundle: ^1.1
- twig/twig: ^2.6
Suggests
- symfony/symfony: For using as a Symfony Bundle and for using a default ajax validation
- 2.0.x-dev
- 1.7.x-dev
- dev-master / 1.7.x-dev
- v1.7.0
- 1.6.x-dev
- 1.6.1
- 1.6
- 1.5.x-dev
- 1.5.1
- 1.5
- 1.4.x-dev
- 1.4.1
- 1.4.0
- 1.3.x-dev
- 1.3.0
- 1.2.x-dev
- 1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-rv-symfony-6
- dev-rv-symfony-7
- dev-rd-fix-badge-url
- dev-sf3.3
- dev-release_1_0_2
This package is auto-updated.
Last update: 2024-10-07 14:27:38 UTC
README
This module enables validation of the Symfony 4 or later forms on the JavaScript side. It converts form type constraints into JavaScript validation rules.
If you have Symfony 4.4 or 5.x - you need to use Version 1.7.x
If you have Symfony 4.* - you need to use Version 1.6.x-dev
If you have Symfony 3.1.* - you need to use Version 1.5.*
If you have Symfony 3.0.* - you need to use Version 1.4.*
If you have Symfony 2.8.* or 2.7.* - you need to use Version 1.3.*
If you have Symfony 2.6.* or less - you need to use Version 1.2.*
1 Installation
1.1 Download FpJsFormValidatorBundle using composer
Run in terminal:
$ composer require "shopsys/jsformvalidator-bundle":"dev-master"
Or if you do not want to unexpected problems better to use exact version.
$ composer require "shopsys/jsformvalidator-bundle":"v1.6.*"
1.2 Enable javascript libraries
There are two ways to initialize javascript's files for this library. You can create a new entry in the webpack or import the main file into your javascript.
1.2.1 Add FpJsFormValidatorBundle to webpack.config.js
Encore
...
.addEntry('app', './assets/js/app.js')
+ .addEntry('FpJsFormElement', './vendor/fp/jsformvalidator-bundle/Fp/JsFormValidatorBundle/Resources/public/js/FpJsFormValidatorWithJqueryInit.js')
...
.configureBabel(null, {
useBuiltIns: 'usage',
corejs: 3,
})
;
And include new entry in your template
+ {{ encore_entry_script_tags('FpJsFormElement') }}
{{ encore_entry_script_tags('app') }}
1.2.2 Import FpJsFormValidatorBundle in your main javascript
import $ from 'jquery'; + import 'path-to-bundles/fpjsformvalidator/js/FpJsFormValidator'; + import 'path-to-bundles/fpjsformvalidator/js/jquery.fpjsformvalidator';
1.2.3 Use inits in your template
{% block javascripts %} + {{ js_validator_config() }} + {{ init_js_validation() }} {% endblock %}
1.4 Add routes
If you use the UniqueEntity constraint, then you have to include the next part to your routing config: app/config/routing.yml
# ... fp_js_form_validator: resource: "@FpJsFormValidatorBundle/Resources/config/routing.xml" prefix: /fp_js_form_validator
Make sure that your security settings do not prevent these routes.
2 Usage
After the previous steps the javascript validation will be enabled automatically for all your forms.
- Disabling validation
- If your forms are placed in sub-requests
- If you need to initialize JS validation for your forms separately, or by some event, in this case you need to follow these steps instead of the chapter 1.3
3 Customization
Preface
This bundle finds related DOM elements for each element of a symfony form and attach to it a special object-validator. This object contains list of properties and methods which fully define the validation process for the related form element. And some of those properties and methods can be changed to customize the validation process.
If you render forms with a some level of customization - read this note.
- Disable validation for a specified field
- Error display
- Get validation groups from a closure
- Getters validation
- The Callback constraint
- The Choice constraint. How to get the choices list from a callback
- Custom constraints
- Custom data transformers
- Checking the uniqueness of entities
- Form submit by Javasrcipt
- onValidate callback
- Run validation on custom event
- Collections validation
4 Local development
Requirements
- docker with docker-compose
- make
Run application
docker-compose up -d --build
make install
# test form will be available at http://127.0.0.1:8080
Run tests
make tests