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

Spain Validator Bundle Laravel Package

avegao/spain-validator-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require avegao/spain-validator-bundle
    

    Register the bundle in app/AppKernel.php:

    new Xinjia\SpainValidatorBundle\XinjiaSpainValidatorBundle(),
    
  2. First Use Case: Validate a DNI in a Symfony form or entity:

    use Xinjia\SpainValidatorBundle\Validator as ExtraAssert;
    
    /**
     * @ExtraAssert\Dni(message="Invalid DNI")
     */
    private $dni;
    

Key Entry Points

  • Entity Validation: Use @ExtraAssert annotations in Doctrine entities.
  • Form Validation: Apply constraints directly in form builder methods.
  • Standalone Validation: Use validators in controllers or services via ValidatorInterface.

Implementation Patterns

Common Workflows

1. Entity Validation

// Entity class
use Xinjia\SpainValidatorBundle\Validator as ExtraAssert;

/**
 * @ExtraAssert\ZipCode(message="Invalid Spanish postal code")
 */
private $postalCode;

2. Form Validation

// Controller
$form = $this->createFormBuilder()
    ->add('postalCode', TextType::class, [
        'constraints' => new ZipCode(),
    ])
    ->getForm();

3. Dynamic Validation

Validate fields programmatically in services:

use Xinjia\SpainValidatorBundle\Validator\Dni;
use Symfony\Component\Validator\Validator\ValidatorInterface;

$validator = $this->get('validator');
$errors = $validator->validate($dniValue, new Dni());

4. Combining Validators

Use AllPhone for flexible phone validation (fixed/mobile):

$form->add('phone', TextType::class, [
    'constraints' => [
        new AllPhone(),
        new Length(['max' => 9]),
    ],
]);

Integration Tips

  • Symfony Forms: Prefer form-level constraints for reusable validation logic.
  • Doctrine: Use entity annotations for database-level validation (e.g., DniCif).
  • APIs: Validate incoming data in request handlers or DTOs.
  • Custom Messages: Override default messages via translations or setMessage().

Gotchas and Tips

Pitfalls

  1. Deprecated Bundle:

    • Last release in 2016 (Symfony 2.6). May conflict with newer Symfony versions.
    • Workaround: Fork the repo or use standalone validators (e.g., symfony/validator + custom rules).
  2. Namespace Mismatch:

    • README shows Xinjia\SpainValidatorBundle, but composer points to avegao/spain-validator-bundle.
    • Fix: Verify autoloader or adjust use statements.
  3. Strict Validation:

    • ZipCode expects 5-digit Spanish postal codes (e.g., 28001). Rejects formats like 28001-MAD.
    • Tip: Pre-process input (e.g., trim() or regex) if needed.
  4. DNI/NIE/CIF Ambiguity:

    • DniCif validates both DNI and CIF but may not distinguish between them.
    • Tip: Use separate Dni/Cif validators for clarity.

Debugging

  • Validator Errors: Check Symfony’s Validator component logs for constraint failures.
  • Test Edge Cases:
    • DNI: 12345678A, 71234567Z (valid); 12345678 (invalid).
    • CIF: B12345678 (valid); 123456789 (invalid).
    • Postal Code: 08001 (valid); 08001-BAR (invalid).

Extension Points

  1. Custom Rules: Extend validators (e.g., add Nie support) by subclassing AbstractValidator:

    class Nie extends AbstractValidator {
        public function isValid($value, Constraint $constraint) {
            // Custom NIE logic
        }
    }
    
  2. Configuration: Override default messages in config/validator/validation.yaml:

    Xinjia\SpainValidatorBundle\Validator\Constraints\Dni:
        message: "El DNI {{ value }} no es válido."
    
  3. Performance: Cache validation results for repeated checks (e.g., in API rate-limited endpoints).

Pro Tips

  • Use in DTOs: Validate input data objects before processing:
    $dto = new UserDto($request->request->all());
    $errors = $validator->validate($dto);
    
  • Symfony 4+: Replace the bundle with standalone constraints or symfony/validator + custom rules.
  • Testing: Mock validators in PHPUnit:
    $validator = $this->createMock(ValidatorInterface::class);
    $validator->method('validate')->willReturn([]);
    $this->container->set('validator', $validator);
    
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.
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
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle