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

Validator Es Bundle Laravel Package

ajgl/validator-es-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install via Composer:
    composer require ajgl/validator-es-bundle
    
  2. Enable the Bundle in config/bundles.php:
    return [
        // ...
        Ajgl\ValidatorEsBundle\AjglValidatorEsBundle::class => ['all' => true],
    ];
    
  3. First Use Case: Validate a Spanish NIF (Número de Identificación Fiscal) in a Symfony form:
    use Symfony\Component\Validator\Constraints as Assert;
    
    $builder->add('nif', null, [
        new Assert\NotBlank(),
        new Assert\AjglNif(), // Custom constraint provided by the bundle
    ]);
    

Key Documentation

  • Check Resources/doc/index.md for validator-specific usage.
  • Default validators include:
    • AjglNif (Spanish tax ID)
    • AjglCif (Company tax ID)
    • AjglIban (Spanish IBAN)
    • AjglPostalCode (Spanish postal codes)

Implementation Patterns

Common Workflows

  1. Form Validation:

    // src/Form/ClientType.php
    use Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif;
    
    $builder->add('taxId', null, [
        new Assert\NotBlank(),
        new AjglNif(['message' => 'Invalid Spanish NIF']),
    ]);
    
  2. Manual Validation:

    use Ajgl\ValidatorEsBundle\Validator\Constraints\AjglCif;
    use Symfony\Component\Validator\Validator\ValidatorInterface;
    
    $validator = $this->get('validator');
    $constraint = new AjglCif();
    $errors = $validator->validate($entity->getCif(), $constraint);
    
  3. Custom Error Messages:

    # config/validation.yaml
    Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif:
        message: 'El NIF "{{ value }}" no es válido. Ejemplo: 12345678A'
    
  4. API Request Validation (Symfony 5+):

    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\Serializer\Annotation\Valid;
    
    #[Assert\Collection([
        new Assert\All([
            new Assert\AjglIban(),
        ]),
    ])]
    public array $transactions;
    

Integration Tips

  • Leverage Symfony’s Validation Groups:

    $validator->validate($entity, null, ['spanish_codes']);
    

    (Define groups in validation.yaml under each constraint.)

  • Reuse in DTOs:

    #[Assert\Type('string')]
    #[Assert\AjglPostalCode()]
    public string $postalCode;
    
  • Extend Existing Validators:

    // src/Validator/Constraints/AjglCustomNif.php
    use Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif;
    
    class AjglCustomNif extends AjglNif {
        public function getTargets() {
            return self::PROPERTY_CONSTRAINT | self::CLASS_CONSTRAINT;
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Constraint Naming Conflicts:

    • If using Symfony’s built-in Iban constraint, explicitly namespace the bundle’s AjglIban:
      new \Ajgl\ValidatorEsBundle\Validator\Constraints\AjglIban()
      
  2. Case Sensitivity:

    • Spanish NIFs/CIFs are case-sensitive (e.g., 12345678A12345678a). The bundle enforces this by default.
  3. Postal Code Format:

    • The AjglPostalCode validator expects 5-digit Spanish postal codes (e.g., 28001). Leading zeros are required (e.g., 08001 for Barcelona).
  4. Bundle Auto-Configuration:

    • If using Symfony Flex, the bundle does not auto-configure. Manually enable it in bundles.php.

Debugging

  • Validate Without Errors:
    $validator->validate($value, new AjglNif(), ['skip_cascade' => true]);
    
  • Check Constraint Metadata:
    php bin/console debug:validator AjglNif
    

Extension Points

  1. Custom Validation Logic:

    // src/Validator/Constraints/AjglCustomCif.php
    use Ajgl\ValidatorEsBundle\Validator\AjglCifValidator;
    use Symfony\Component\Validator\Constraint;
    use Symfony\Component\Validator\ConstraintValidator;
    
    class AjglCustomCifValidator extends ConstraintValidator {
        public function validate($value, Constraint $constraint) {
            if (!$value instanceof AjglCustomCif) {
                return;
            }
            // Extend AjglCifValidator logic here
        }
    }
    
  2. Override Default Messages:

    # config/packages/validator.yaml
    Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif:
        message: '{{ value }} es un NIF inválido. Formato: 8 dígitos + letra.'
    
  3. Add New Validators:

    • Extend Ajgl\ValidatorEsBundle\Validator\AbstractSpanishCodeValidator and register the constraint in the bundle’s Resources/config/validation.xml.

Performance Tips

  • Cache Validators: Symfony’s validator component caches constraints by default. No additional setup is needed.
  • Batch Validation: For bulk validation (e.g., CSV imports), use:
    $validator->validate($batchData, null, ['spanish_codes'], true); // $isCascading = true
    

Configuration Quirks

  • Locale-Specific Rules: The bundle assumes Spanish locale rules. For mixed-language apps, validate locale-specific fields separately.
  • Symfony 6+: If using Symfony 6, ensure symfony/validator is pinned to ^5.4 (the bundle’s require targets Symfony 2.1+ but works with modern versions).
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