Using PHP Attributes to Create and Use a Custom Validator in Symfony

using-php-attributes-to-create-and-use-a-custom-validator-in-symfony

Symfony, a leading PHP framework, is consistently updated to leverage modern PHP features. With PHP 8, attributes provide a new way to define metadata for classes, methods, properties, etc., which can be used for validation constraints. This blog post will guide you through creating and using a custom validator in Symfony to validate UK mobile number prefixes using PHP attributes.

What Are PHP Attributes?

PHP attributes, introduced in PHP 8, enable you to add metadata to various code elements, accessible via reflection. In Symfony, attributes can simplify defining validation constraints, making your code more concise and readable.

Example 1 – Creating a Custom Validator for UK Mobile Number Prefix

Let’s create a custom validator to check if a phone number has a valid UK mobile prefix (e.g., starting with ’07’).

Step 1: Define the Attribute

Create a new attribute class that defines the custom constraint.

// src/Validator/Constraints/UkMobile.php
namespace AppValidatorConstraints;

use Attribute;
use SymfonyComponentValidatorConstraint;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
class UkMobile extends Constraint
{
    public string $message = 'The number "{{ string }}" is not a valid UK mobile number.';
}

Step 2: Create the Validator

Next, create the validator with the logic to check if a phone number has a valid UK mobile prefix.

// src/Validator/Constraints/UkMobileValidator.php
namespace AppValidatorConstraints;

use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorConstraintValidator;
use SymfonyComponentValidatorExceptionUnexpectedTypeException;

class UkMobileValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint): void
    {
        if (null === $value || '' === $value) {
            return;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }

        // Check if the number starts with '07'
        if (!preg_match('/^07[0-9]{9}$/', $value)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ string }}', $value)
                ->addViolation();
        }
    }
}

Step 3: Apply the Attribute in an Entity

Use the UkMobile attribute in your entities to enforce this custom validation rule.

// src/Entity/User.php
namespace AppEntity;

use AppValidatorConstraints as AppAssert;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;

#[ORMEntity]
class User
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'string', length: 15)]
    #[AssertNotBlank]
    #[AppAssertUkMobile]
    private $mobileNumber;

    // getters and setters
}

Step 4: Test the Validator

Ensure everything works correctly by writing some unit tests or using Symfony’s built-in validation mechanism.

// tests/Validator/Constraints/UkMobileValidatorTest.php
namespace AppTestsValidatorConstraints;

use AppValidatorConstraintsUkMobile;
use AppValidatorConstraintsUkMobileValidator;
use SymfonyComponentValidatorTestConstraintValidatorTestCase;

class UkMobileValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): ConstraintValidator
    {
        return new UkMobileValidator();
    }

    public function testNullIsValid(): void
    {
        $this->validator->validate(null, new UkMobile());

        $this->assertNoViolation();
    }

    public function testValidUkMobileNumber(): void
    {
        $this->validator->validate('07123456789', new UkMobile());

        $this->assertNoViolation();
    }

    public function testInvalidUkMobileNumber(): void
    {
        $constraint = new UkMobile();

        $this->validator->validate('08123456789', $constraint);

        $this->buildViolation($constraint->message)
            ->setParameter('{{ string }}', '08123456789')
            ->assertRaised();
    }
}

Example 2 – Creating a Custom Validator for Glasgow Postcodes

In this example, we want to create a custom validator to check if a postcode is a valid Glasgow postcode. This could be used for professional trade services i.e. bark.com where a company only services certain areas.

Step 1: Define the Attribute

First, create a new attribute class to define the custom constraint.

// src/Validator/Constraints/GlasgowPostcode.php
namespace AppValidatorConstraints;

use Attribute;
use SymfonyComponentValidatorConstraint;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
class GlasgowPostcode extends Constraint
{
    public string $message = 'The postcode "{{ string }}" is not a valid Glasgow postcode.';
}

Step 2: Create the Validator

Next, create the validator with the logic to check if a postcode is a valid Glasgow postcode.

// src/Validator/Constraints/GlasgowPostcodeValidator.php
namespace AppValidatorConstraints;

use SymfonyComponentValidatorConstraint;
use SymfonyComponentValidatorConstraintValidator;
use SymfonyComponentValidatorExceptionUnexpectedTypeException;

class GlasgowPostcodeValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint): void
    {
        if (null === $value || '' === $value) {
            return;
        }

        if (!is_string($value)) {
            throw new UnexpectedTypeException($value, 'string');
        }

        // Regex for validating Glasgow postcodes (starting with G)
        $pattern = '/^Gd{1,2}s?d[A-Z]{2}$/i';

        if (!preg_match($pattern, $value)) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ string }}', $value)
                ->addViolation();
        }
    }
}

Step 3: Apply the Attribute in an Entity

Use the GlasgowPostcode attribute in your entities to enforce this custom validation rule.

// src/Entity/Address.php
namespace AppEntity;

use AppValidatorConstraints as AppAssert;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;

#[ORMEntity]
class Address
{
    #[ORMId]
    #[ORMGeneratedValue]
    #[ORMColumn(type: 'integer')]
    private $id;

    #[ORMColumn(type: 'string', length: 10)]
    #[AssertNotBlank]
    #[AppAssertGlasgowPostcode]
    private $postcode;

    // getters and setters
}

Step 4: Test the Validator

Ensure everything works correctly by writing some unit tests or using Symfony’s built-in validation mechanism.

// tests/Validator/Constraints/GlasgowPostcodeValidatorTest.php
namespace AppTestsValidatorConstraints;

use AppValidatorConstraintsGlasgowPostcode;
use AppValidatorConstraintsGlasgowPostcodeValidator;
use SymfonyComponentValidatorTestConstraintValidatorTestCase;

class GlasgowPostcodeValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): ConstraintValidator
    {
        return new GlasgowPostcodeValidator();
    }

    public function testNullIsValid(): void
    {
        $this->validator->validate(null, new GlasgowPostcode());

        $this->assertNoViolation();
    }

    public function testValidGlasgowPostcode(): void
    {
        $this->validator->validate('G1 1AA', new GlasgowPostcode());

        $this->assertNoViolation();
    }

    public function testInvalidGlasgowPostcode(): void
    {
        $constraint = new GlasgowPostcode();

        $this->validator->validate('EH1 1AA', $constraint);

        $this->buildViolation($constraint->message)
            ->setParameter('{{ string }}', 'EH1 1AA')
            ->assertRaised();
    }
}

Beyond Entities

Custom validators aren’t restricted to entities. They can be used to apply validation to properties and methods of any class you need, for example, if we wanted to use the GlasgowPostcode validator in a DTO object we could do something like

// src/DTO/PostcodeDTO.php
namespace AppDTO;

use AppValidatorConstraints as AppAssert;
use SymfonyComponentValidatorConstraints as Assert;

class PostcodeDTO
{
    #[AssertNotBlank]
    #[AppAssertGlasgowPostcode]
    private string $postcode;

    public function __construct(string $postcode)
    {
        $this->postcode = $postcode;
    }

    public function getPostcode(): string
    {
        return $this->postcode;
    }
}

and to check this DTO contains valid data we would make use of the validation service like

$postcodeDTO = new PostcodeDTO('G1 1AA');
$violations = $this->validator->validate($postcodeDTO);

Conclusion

Using PHP attributes to define custom validators in Symfony can enhance code readability and leverage modern PHP features. By following the steps outlined above, you can create robust, reusable validation logic that integrates seamlessly with Symfony’s validation system. This approach simplifies adding custom validations and keeps your code clean and maintainable.

Happy coding!

Originally published at https://chrisshennan.com/blog/using-php-attributes-to-create-and-use-a-custom-validator-in-symfony

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
wingpg-version-released

WinGPG version Released

Next Post
pharmaceutical-software-development:-a-comprehensive-guide

Pharmaceutical Software Development: A Comprehensive Guide

Related Posts