c0ntax/parsley-bundle
Symfony bundle that maps Symfony Form constraints and entity annotations to Parsley.js data-parsley-* attributes for client-side validation. Includes basic configuration (enable/trigger) and supports Email, Length, Pattern, Min/Max, Required, and Range.
Illuminate\Validation\Validator) is incompatible with Symfony’s Constraint system, requiring custom adapters or middleware to bridge the gap.data-parsley-*). This aligns with Laravel’s validation rules (e.g., Rule::minLength()), but Laravel lacks Symfony’s constraint-based system, necessitating manual mapping or a custom wrapper.DirectiveInterface, allowing TPMs to extend functionality (e.g., adding Laravel-specific validators like unique or confirmed). However, this requires developer effort to implement and maintain.FormBuilder, which is not natively available in Laravel. Workarounds include:
symfony/form package).Illuminate\Validation\Rule → data-parsley-*).data-parsley-* attributes into Blade/Vue/React components.| Risk Area | Symfony Risk | Laravel Risk | Mitigation Strategy |
|---|---|---|---|
| Validation Mismatch | Low | Medium | Test edge cases (e.g., Range, Pattern). |
| Parsley.js Dependency | Low | Medium | Bundle Parsley.js via npm/yarn or CDN. |
| Form Component Gap | None | High | Use Symfony Form Component or build adapter. |
| Twig vs. Blade | None | Medium | Abstract attribute injection logic. |
| Performance Overhead | Low | Low | Minify Parsley.js; lazy-load if needed. |
| Maintenance Burden | Low | High (Laravel) | Contribute to package or fork for Laravel. |
| Deprecated Features | Low | N/A | Monitor Parsley.js/Symfony updates. |
Illuminate\Validation or Symfony integration)?Callback, Expression) that require custom directives?| Component | Symfony Fit | Laravel Fit | Workaround |
|---|---|---|---|
| Form Handling | Native | Partial (Symfony Form) | Use symfony/form or build adapter. |
| Validation | Native | Partial (Illuminate\Validation) |
Map Laravel rules to Parsley.js. |
| Templating | Twig | Blade/Vue/React | Abstract attribute injection. |
| Parsley.js | CDN/npm | CDN/npm | Bundle via Laravel Mix. |
| Dependency Mgmt | Composer | Composer/npm | Use symfony/form or custom package. |
composer require c0ntax/parsley-bundle
# config/packages/c0ntax_parsley.yaml
c0ntax_parsley:
enabled: true
field:
trigger: blur
// config/bundles.php
C0ntax\ParsleyBundle\C0ntaxParsleyBundle::class => ['all' => true],
parsleys option in FormBuilder.<script src="https://cdn.jsdelivr.net/npm/parsleyjs@latest/dist/parsley.min.js"></script>
$(document).ready(function() { $('form').parsley(); });
Option A: Symfony Form Component (Highest Compatibility)
composer require symfony/form symfony/validator
Option B: Custom Laravel Adapter (Lightweight)
// app/Services/ParsleyValidator.php
class ParsleyValidator {
public static function getParsleyAttributes(array $rules): array {
$mapping = [
'required' => 'data-parsley-required="true"',
'min' => fn($len) => sprintf('data-parsley-minlength="%d"', $len),
// Add other mappings...
];
$attributes = [];
foreach ($rules as $rule => $value) {
if (isset($mapping[$rule])) {
$attributes[] = is_callable($mapping[$rule])
? call_user_func($mapping[$rule], $value)
: $mapping[$rule];
}
}
return $attributes;
}
}
FormRequest or use a form builder (e.g., Laravel Collective HTML) to inject attributes:
// In a FormRequest or controller
$parsleyAttrs = ParsleyValidator::getParsleyAttributes($request->rules());
return view('form', ['parsley_attrs' => $parsleyAttrs]);
<input type="text" {{ $parsley_attrs }}>
// resources/js/app.js
import 'parsleyjs/dist/parsley.min.js';
document.addEventListener('DOMContentLoaded', () => {
$('form').parsley();
});
Option C: Manual Attribute Injection (High Flexibility)
@parsley(['required', 'min:3'])
<input type="text
How can I help you explore Laravel packages today?