The Javascript validation for Symfony 2, 3 and 4 forms
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.* - you need to use Version 1.6.x-dev
If you have Symfony 3.1. - you need to use [Version 1.5.](https://github.com/formapro/JsFormValidatorBundle/tree/1.5)
If you have Symfony 3.0. - you need to use [Version 1.4.](https://github.com/formapro/JsFormValidatorBundle/tree/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.](https://github.com/formapro/JsFormValidatorBundle/tree/1.2)
Run in terminal:
$ composer require "fp/jsformvalidator-bundle":"dev-master"
Or if you do not want to unexpected problems better to use exact version.
$ composer require "fp/jsformvalidator-bundle":"v1.6.*"
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.
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') }}
import $ from 'jquery';
+ import 'path-to-bundles/fpjsformvalidator/js/FpJsFormValidator';
+ import 'path-to-bundles/fpjsformvalidator/js/jquery.fpjsformvalidator';
{% block javascripts %}
+ {{ js_validator_config() }}
+ {{ init_js_validation() }}
{% endblock %}
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.
After the previous steps the javascript validation will be enabled automatically for all your forms.
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.