Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Symfony Validator Constraints Bundle Laravel Package

halloverden/symfony-validator-constraints-bundle

A Symfony bundle that integrates HalloVerden's validator constraints, registering them as services and making custom validation rules available throughout your application with minimal configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

This is a Symfony bundle, so the first decision is whether it belongs in your stack at all. If you are working in a Laravel codebase, you cannot composer require your way to value here — Laravel's validator is illuminate/validation, and this bundle's only job is Symfony DI wiring around constraint classes. The realistic Laravel path is to pull in symfony/validator standalone and use the constraint objects directly (see Implementation Patterns), skipping the bundle entirely.

For a Symfony app:

composer require halloverden/symfony-validator-constraints-bundle

Then register it. On Symfony 5.4+/6.x with Flex this is usually automatic via the recipe; otherwise add it to config/bundles.php:

return [
    // ...
    Halloverden\SymfonyValidatorConstraintsBundle\SymfonyValidatorConstraintsBundle::class => ['all' => true],
];

Clear the cache (bin/console cache:clear) before you conclude anything is broken — new constraint validators are service-registered and will not appear until the container is rebuilt.

Where to look first: src/Constraint/. With 2 stars and 0 dependents there is no meaningful documentation trail, so the source tree is the documentation. Each usable constraint is a pair: a Constraint subclass (the attribute you'll write) and a ConstraintValidator subclass (the logic). The tests/ directory is the second stop — for packages this small, the test suite is the only reliable statement of intended behavior and edge cases.

First use case: pick your lowest-risk validation — something cosmetic like a phone number or postal code field on a profile form — not a payment or identity field. Validate it in isolation with a throwaway test before wiring it into an entity:

$validator = Validation::createValidatorBuilder()
    ->enableAttributeMapping()
    ->getValidator();

$violations = $validator->validate($someValue, new \Halloverden\...\SomeConstraint());

If that returns violations as expected, proceed to attribute usage on a DTO.

Implementation Patterns

Attribute-on-DTO, validated at the boundary. The highest-leverage pattern is to keep constraints off Eloquent/Doctrine entities and on request DTOs, so validation is explicit and testable:

final class UpdateProfileRequest
{
    #[Assert\NotBlank]
    #[YourBundleConstraint(message: 'profile.phone.invalid')]
    public string $phone = '';
}

Compose with core constraints rather than trusting the bundle's constraint to reject empty input — most custom constraints validate format, not presence. NotBlank + the bundle constraint is almost always the correct pair.

Form types. Pass the constraint through the constraints option alongside the field type; this is where most Symfony teams actually consume these:

->add('phone', TextType::class, [
    'constraints' => [new Assert\NotBlank(), new YourBundleConstraint()],
])

Validation groups. If you only need the constraint on one flow, give it a group (#[YourBundleConstraint(groups: ['registration'])]) rather than adding conditional logic around the validator call. This is the cheapest way to scope a low-confidence third-party constraint.

The Laravel bridge, if you must. Since the constraint classes are usually plain symfony/validator citizens, you can composer require symfony/validator and instantiate them inside a custom Laravel rule:

class SymfonyConstraintRule implements ValidationRule
{
    public function __construct(private Constraint $constraint) {}

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $validator = Validation::createValidator();
        $violations = $validator->validate($value, $this->constraint);

        foreach ($violations as $violation) {
            $fail($violation->getMessage());
        }
    }
}

This is more ceremony than writing the regex yourself, so only do it if the constraint encodes genuinely non-trivial logic (checksum algorithms, national ID formats). Otherwise the dependency is pure cost.

Testing. Use ConstraintValidatorTestCase from symfony/validator to test the constraint against your own fixtures, not the package's. You want your test suite to fail loudly if an upstream update changes accepted formats:

class PhoneConstraintValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): ConstraintValidatorInterface
    {
        return new PhoneConstraintValidator();
    }
}

Gotchas and Tips

Bus factor is the headline risk. Two stars, zero dependents, no visible release cadence. Treat every constraint you adopt as code you now own. Before shipping, copy the validator logic into your own App\Validator namespace if it's short — a 40-line checksum validator is cheaper to vendor than to depend on. If you keep the dependency, pin the version in composer.json and add a composer outdated check to CI.

Validator services must be registered. Custom constraints fail with ConstraintValidator ... does not exist when the validator class isn't tagged as a service. Check that the bundle's DependencyInjection/ extension registers validators with validator.constraint_validator, or that services.yaml autoconfigures ConstraintValidatorInterface. If the bundle predates autoconfiguration, you may need to register validators manually in your app's services.yaml. This is the single most common "it doesn't work" cause with small Symfony bundles.

Messages are untranslated. Custom constraints typically ship English defaults (or hardcoded strings) with no translation catalogue. Plan to override via translations/validators.en.yaml using the message keys the constraint exposes, and verify the constraint sets a translationDomain at all — some don't, in which case messages pass through raw.

Check the version constraints before you plan an upgrade. Small bundles frequently cap at symfony/validator: ^4.4|^5.0 and go stale. Read composer.json for both PHP and Symfony ranges, and confirm attribute support (#[...]) exists — annotation-only constraints force doctrine/annotations on you and won't work cleanly with PHP 8 attributes or Symfony 6+ defaults.

Debugging. bin/console debug:validator 'App\DTO\UpdateProfileRequest' prints the resolved constraint graph for a class — the fastest way to confirm your attribute is actually being read and that metadata caching isn't serving a stale result. If constraints mysteriously vanish, clear var/cache before anything else.

Extension points worth knowing. Rather than forking, you can (a) subclass the constraint and swap the validatedBy() target with your own validator, or (b) decorate the bundle's validator service to post-process results. Both are lower-maintenance than a hard fork and make upstream updates safe.

Write adversarial fixtures. Locale-specific or identifier constraints are where small packages are weakest — edge cases like leading zeros, whitespace, lowercase input, and boundary lengths are rarely covered. Pull the test fixtures from tests/ first, then extend them with cases from your own domain before trusting the constraint in production.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator