boekkooi/jquery-validation-bundle
Installation:
composer require boekkooi/jquery-validation-bundle
Enable the bundle in config/bundles.php:
Boekkooi\JqueryValidationBundle\BoekkooiJqueryValidationBundle::class => ['all' => true],
Basic Usage:
Add the validation script to your base template (e.g., base.html.twig):
{{ jquery_validation_bundle_js() }}
First Use Case: Validate a form in a Twig template:
{{ form_start(form) }}
{{ form_widget(form) }}
<button type="submit" class="validate">Submit</button>
{{ form_end(form) }}
Ensure the form has a validate class or use data-validate="true" on the submit button.
config/packages/boekkooi_jquery_validation.yaml:
boekkooi_jquery_validation:
default_rules: { email: { required: true, email: true } }
Override default rules per form in Twig:
{{ form_start(form, { 'attr': { 'data-validation-rules': '{"username": {"required": true, "minlength": 5}}' } }) }}
Pass rules dynamically from a controller:
return $this->render('form.html.twig', [
'form' => $form->createView(),
'validation_rules' => json_encode(['password' => ['required' => true, 'minlength' => 8]]),
]);
Use in Twig:
{{ form_start(form, { 'attr': { 'data-validation-rules': validation_rules } }) }}
Define custom messages in Twig:
{{ form_start(form, {
'attr': {
'data-validation-messages': '{
"username": {"required": "Please enter your username"},
"email": {"email": "Invalid email format"}
}'
}
}) }}
Use Symfony form errors to pre-populate validation errors:
{% for error in form.errors %}
<div class="error">{{ error.message }}</div>
{% endfor %}
{{ encore_entry_link_tags('app') }} {# Webpack Encore #}
{{ jquery_validation_bundle_js() }}
public_path() in Twig to reference assets if needed:
<script src="{{ asset('bundles/boekkoojqueryvalidation/js/jquery.validation.js') }}"></script>
// In a service or controller
$twig->addExtension(new \Your\Custom\ValidationExtension());
Outdated Dependencies:
symfony/form, twig) manually.Asset Path Conflicts:
config/packages/boekkooi_jquery_validation.yaml:
boekkooi_jquery_validation:
js_path: '/build/bundle.js' # Custom path
Twig Template Caching:
data-validation-rules may not update if Twig cache is aggressive.{{ form_start(form, { 'attr': { 'data-validation-rules': validation_rules|raw } }) }} to bypass escaping issues.jQuery Version Mismatch:
// Add this to your custom JS
$.validator.setDefaults({ ignore: '' }); // Fix for jQuery 3+
Console Errors:
Uncaught ReferenceError or 404 on JS/CSS files.npm run dev).Validation Not Triggering:
class="validate" or data-validate="true".console.log("Validation initialized"); to the bundle’s JS file.Rules Not Applying:
data-validation-rules:
console.log(JSON.parse('{"field": {"rule": true}}')); // Test in browser console
Custom Validation Methods:
methods object in a custom JS file:
$.validator.addMethod("custom_rule", function(value, element) {
return value.length > 10; // Example
}, "Value must be longer than 10 characters");
data-validation-rules='{"field": {"custom_rule": true}}'
Override Bundle Templates:
Resources/views/Form/validation.html.twig to your theme and modify.Symfony Event Subscribers:
kernel.request to dynamically inject rules:
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if ($request->attributes->get('_route') === 'custom_route') {
$request->attributes->set('validation_rules', ['field' => ['required' => true]]);
}
}
app.request.attributes.validation_rules.{% block %}:
{% block validation_js %}
{{ jquery_validation_bundle_js() }}
{% endblock %}
How can I help you explore Laravel packages today?