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

Input Handler Bundle Laravel Package

coka/input-handler-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Run composer require coka/input-handler-bundle in your Laravel project (or Symfony if adapted). No additional configuration is required for basic usage.

  2. First Use Case Use the bundle to validate incoming request data with custom constraints. For example, in a Laravel controller:

    use Coka\InputHandlerBundle\Validator\Constraints as InputAssert;
    
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => [
                'required',
                new InputAssert\EmailFormat(), // Custom constraint
            ],
        ]);
    
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 422);
        }
    }
    
  3. Where to Look First

    • Documentation: Check the GitHub README for constraint examples.
    • Constraints: Explore src/Validator/Constraints for built-in constraints like EmailFormat, PhoneNumber, or CustomRegex.
    • Service Provider: Override default behavior in config/packages/coka_input_handler.yaml (if available).

Implementation Patterns

Core Workflows

  1. Request Validation Integrate constraints into Laravel’s built-in Validator:

    $validator = Validator::make($data, [
        'field' => new InputAssert\CustomConstraint(),
    ]);
    
  2. Custom Constraints Extend existing constraints or create new ones:

    namespace App\Validator\Constraints;
    
    use Coka\InputHandlerBundle\Validator\Constraints\AbstractConstraint;
    
    class CustomConstraint extends AbstractConstraint
    {
        protected function validate($value, Constraint $constraint)
        {
            return $value === 'expected';
        }
    }
    
  3. API Responses Format validation errors for API responses:

    if ($validator->fails()) {
        return response()->json([
            'success' => false,
            'errors' => $validator->errors()->toArray(),
        ], 422);
    }
    
  4. Form Requests Use in FormRequest classes for reusable validation:

    public function rules()
    {
        return [
            'username' => ['required', new InputAssert\UsernameFormat()],
        ];
    }
    

Integration Tips

  • Symfony Compatibility: If using Symfony, leverage the bundle’s native integration with Symfony’s Validator component.
  • Laravel Service Providers: Bind custom constraints to Laravel’s validator:
    Validator::extend('custom_constraint', function ($attribute, $value, $parameters) {
        return app()->make(\App\Validator\Constraints\CustomConstraint::class)
                    ->validate($value, new Constraint());
    });
    
  • Testing: Mock constraints in PHPUnit:
    $validator = Validator::make($data, ['field' => new InputAssert\MockConstraint()]);
    

Gotchas and Tips

Pitfalls

  1. Constraint Registration

    • If custom constraints aren’t recognized, ensure they’re autoloaded (check composer.json).
    • For Laravel, manually register constraints in a service provider if autoloading fails.
  2. Performance

    • Avoid overly complex constraints in loops (e.g., regex validation on large datasets). Cache results if possible.
  3. Error Messages

    • Custom constraints may not include default error messages. Override them:
      $validator->extend('custom_constraint', function ($attribute, $value, $parameters, $validator) {
          $validator->errors()->add($attribute, 'Custom error message.');
      });
      
  4. Bundle Maturity

    • Limited adoption (0 stars/dependents). Test thoroughly in staging before production.

Debugging

  • Constraint Validation Logic Use dd($constraint) to inspect passed parameters or values during validation.
  • Symfony Validator Events Listen to validator.error events (Symfony) to debug constraint failures:
    $validator->addListener('validator.error', function ($event) {
        \Log::debug('Validation error:', [$event->getConstraint(), $event->getValue()]);
    });
    

Extension Points

  1. Add Custom Constraints Extend AbstractConstraint and place in app/Validator/Constraints (or a custom namespace).

  2. Override Default Constraints Replace bundle constraints by publishing config (if available) or overriding classes in app/.

  3. API Error Formatting Create a custom AppServiceProvider to standardize error responses:

    Validator::after(function ($validator) {
        if ($validator->fails()) {
            response()->json(['errors' => $validator->errors()], 422)->send();
        }
    });
    
  4. Localization Translate constraint messages via Laravel’s localization system:

    'validation.custom_constraint' => 'The :attribute must meet custom rules.',
    
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor