Weave Code
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

Validation Bundle Laravel Package

antkowiak/validation-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require antkowiak/validation-bundle dev-master
    

    Add the bundle to config/bundles.php:

    Antkowiak\ValidationBundle\AntkowiakValidationBundle::class => ['all' => true],
    
  2. First Use Case Define a class with validation rules (e.g., using annotations or YAML/XML constraints). Example:

    use Symfony\Component\Validator\Constraints as Assert;
    
    class User {
        /**
         * @Assert\NotBlank(message="Ta wartość nie powinna być pusta.")
         * @Assert\Email(message="Ta wartość nie jest prawidłowym adresem email.")
         */
        public $email;
    
        /**
         * @Assert\Length(min=3, minMessage="Ta wartość jest zbyt krótka.")
         */
        public $name;
    }
    
  3. Validate an Object Inject the validator service (via dependency injection or container):

    $validator = $this->container->get('antkowiak.validator');
    $user = new User();
    $user->email = 'invalid';
    $user->name = 'ab';
    
    $isValid = $validator->isValid($user); // false
    $errors = $validator->getMessages($user); // Array of error messages
    

Implementation Patterns

Workflows

  1. Form Validation Use the validator in controllers or form handlers:

    public function handleSubmit(Request $request) {
        $user = new User();
        $user->email = $request->get('email');
        $user->name = $request->get('name');
    
        $validator = $this->get('antkowiak.validator');
        if (!$validator->isValid($user)) {
            $errors = $validator->getMessages($user);
            // Flash errors or render form with errors
        }
        // Proceed on success
    }
    
  2. API Validation Return structured error responses:

    $response = [
        'success' => $validator->isValid($data),
        'errors' => $validator->getMessages($data)
    ];
    return new JsonResponse($response);
    
  3. Custom Validation Logic Extend the validator or create custom constraints:

    use Symfony\Component\Validator\Constraint;
    
    class CustomConstraint extends Constraint {
        public $message = 'Custom validation failed.';
    }
    

Integration Tips

  • Symfony Forms: Integrate with Symfony\Component\Form\FormError for seamless form handling.
  • Event Listeners: Validate objects during lifecycle events (e.g., PRE_PERSIST in Doctrine).
  • Dependency Injection: Prefer constructor injection for the validator service:
    public function __construct(private ValidatorInterface $validator) {}
    

Gotchas and Tips

Pitfalls

  1. Bundle Maturity

    • The package is in dev-master and lacks stars/dependents. Test thoroughly in staging.
    • No official Symfony 6+ support (PHP 7.0+ only; Symfony 5+ may require adjustments).
  2. Error Message Localization

    • Messages are hardcoded in Polish. Override them via constraints or use Symfony’s translator:
      $constraint = new NotBlank(['message' => $this->translator->trans('validation.not_blank')]);
      
  3. Constraint Loading

    • Ensure constraints (annotations/YAML/XML) are properly loaded. For annotations, add:
      # config/packages/antkowiak_validation.yaml
      antkowiak_validation:
        validation:
            enabled_annotations: true
      

Debugging

  • Validator Not Found: Verify the bundle is enabled and the service antkowiak.validator exists.
    php bin/console debug:container antkowiak.validator
    
  • Constraint Not Applied: Check for typos in constraint names or missing annotations.

Extension Points

  1. Custom Validators Implement Symfony\Component\Validator\Validator\ValidatorInterface and register as a service:

    services:
        app.custom_validator:
            class: App\Validator\CustomValidator
            tags: [validator.constraint_validator]
    
  2. Override Default Validator Replace the default validator in config/packages/antkowiak_validation.yaml:

    antkowiak_validation:
        validator: app.custom_validator
    
  3. Group Validation Use Symfony’s validation groups (e.g., Default, Create) via constraints:

    /**
     * @Assert\NotBlank(groups={"Create"})
     */
    public $field;
    

    Then validate with:

    $validator->validate($object, ['Create']);
    
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.
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed