composer require ajgl/validator-es-bundle
config/bundles.php:
return [
// ...
Ajgl\ValidatorEsBundle\AjglValidatorEsBundle::class => ['all' => true],
];
use Symfony\Component\Validator\Constraints as Assert;
$builder->add('nif', null, [
new Assert\NotBlank(),
new Assert\AjglNif(), // Custom constraint provided by the bundle
]);
Resources/doc/index.md for validator-specific usage.AjglNif (Spanish tax ID)AjglCif (Company tax ID)AjglIban (Spanish IBAN)AjglPostalCode (Spanish postal codes)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']),
]);
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);
Custom Error Messages:
# config/validation.yaml
Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif:
message: 'El NIF "{{ value }}" no es válido. Ejemplo: 12345678A'
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;
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;
}
}
Constraint Naming Conflicts:
Iban constraint, explicitly namespace the bundle’s AjglIban:
new \Ajgl\ValidatorEsBundle\Validator\Constraints\AjglIban()
Case Sensitivity:
12345678A ≠ 12345678a). The bundle enforces this by default.Postal Code Format:
AjglPostalCode validator expects 5-digit Spanish postal codes (e.g., 28001). Leading zeros are required (e.g., 08001 for Barcelona).Bundle Auto-Configuration:
bundles.php.$validator->validate($value, new AjglNif(), ['skip_cascade' => true]);
php bin/console debug:validator AjglNif
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
}
}
Override Default Messages:
# config/packages/validator.yaml
Ajgl\ValidatorEsBundle\Validator\Constraints\AjglNif:
message: '{{ value }} es un NIF inválido. Formato: 8 dígitos + letra.'
Add New Validators:
Ajgl\ValidatorEsBundle\Validator\AbstractSpanishCodeValidator and register the constraint in the bundle’s Resources/config/validation.xml.$validator->validate($batchData, null, ['spanish_codes'], true); // $isCascading = true
symfony/validator is pinned to ^5.4 (the bundle’s require targets Symfony 2.1+ but works with modern versions).How can I help you explore Laravel packages today?