acseo/form-js-validation-bundle
Symfony bundle that adds client-side JavaScript validation to Symfony forms. Choose between formvalidation.io or jqueryvalidation.org, enable the service in parameters.yml, call addJsValidation() in controllers, and initialize validation in your Twig template.
Installation:
composer require acseo/form-js-validation-bundle
Register the bundle in AppKernel.php:
new ACSEO\Bundle\FormJsValidationBundle\ACSEOFormJsValidationBundle(),
Configure Validation Plugin:
Choose either validation.io or jqueryvalidation.org in config/parameters.yml:
parameters:
acseo_form_js_validation.service: acseo_form_js_validation_io # or acseo_form_jquery_form_validator
First Use Case: In a controller, add JS validation to a form:
$form = $this->createForm(AwesomeEntityType::class, $entity);
$form = $this->get('acseo_form_js_validation.service')->addJsValidation($form);
return $this->render('template.html.twig', ['form' => $form->createView()]);
Template Setup:
validation.io, include its JS/CSS in your base template:
<link rel="stylesheet" href="https://formvalidation.io/css/formValidation.min.css">
<script src="https://formvalidation.io/js/formValidation.min.js"></script>
jqueryvalidation.org, include jQuery and the validator:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>
Form Integration:
addJsValidation() on any Symfony form instance (e.g., FormType, EntityType).form_div_layout.html.twig).Dynamic Validation Rules:
@Assert\NotBlank, @Assert\Email) to JS validation rules.NotBlank constraint becomes data-fv-notempty="true" in HTML.Customization:
{{ form_widget(form.field, {'attr': {'data-fv-remote': 'true', 'data-fv-remote-url': path('check_field')}}) }}
Event-Driven Extensions:
form.pre_set_data or form.post_bind to dynamically modify validation rules:
$form->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
$form->add('custom_field', TextType::class, [
'constraints' => [new CustomConstraint()],
]);
});
Asset Management:
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('app') }} {# If using Webpack Encore #}
{% endblock %}
Outdated Dependencies:
Missing JS/CSS:
validation.io or jQuery/jQuery Validate JS/CSS will break validation.Constraint Mismatches:
@Assert\Callback) may not map cleanly to JS.data-fv-custom attributes for custom rules:
{{ form_widget(form.field, {'attr': {'data-fv-custom': 'function(value) { return value > 10; }'}}) }}
Service Configuration:
parameters.yml setting (e.g., typo in service name) will throw ServiceNotFoundException.bin/console debug:container acseo_form_js_validation.service to verify the service is registered.Form Theme Conflicts:
form_row with explicit attributes:
{{ form_row(form.field, {'attr': {'data-fv-required': 'true'}}) }}
Inspect Generated HTML:
data-fv-* are rendered correctly.NotBlank field should have data-fv-notempty="true".Console Logs:
validation.io, check browser console for errors like:
FormValidation: No form found with id "form-id"
id (Symfony adds this by default).Remote Validation:
data-fv-remote, ensure the endpoint returns:
{"valid": true/false}
Custom Validators:
// src/Service/CustomValidator.php
class CustomValidator extends AbstractValidator
{
protected function getJsRules(array $constraints): array
{
$rules = parent::getJsRules($constraints);
$rules['custom_field'] = ['data-fv-custom': '...'];
return $rules;
}
}
services.yaml:
services:
acseo_form_js_validation.service:
class: App\Service\CustomValidator
tags: ['acseo.form_js_validation.validator']
Dynamic Rule Injection:
form_widget with custom attributes:
{{ form_widget(form.field, {
'attr': {
'data-fv-remote': 'true',
'data-fv-remote-url': path('validate_field', {'_locale': app.request.locale}),
'data-fv-remote-message': 'Field is already taken'
}
}) }}
Localization:
translations/messages.en.yml:
validation:
not_blank: "This field cannot be empty."
<script>
FormValidation.formValidation(document.getElementById('form-id'), {
fields: {
field_name: {
validators: {
notEmpty: { message: '{{ 'validation.not_blank'|trans }}' }
}
}
}
});
</script>
How can I help you explore Laravel packages today?