scripthoodie/crud-symfony-validator

Implementation for CRUD validator using Symfony validator.

Maintainers

Package info

codeberg.org/scripthoodie/crud-symfony-validator

pkg:composer/scripthoodie/crud-symfony-validator

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

0.3.0 2026-04-18 12:09 UTC

This package is not auto-updated.

Last update: 2026-04-19 07:19:32 UTC


README

Implementation for CRUD validator using Symfony validator.

Install

Via Composer

$ composer require scripthoodie/crud-symfony-validator

Usage

Independent Usage

You can use the validator independently of any CRUD operations by validating entities directly:

First, create a Symfony validator instance with attribute mapping enabled:

use Symfony\Component\Validator\Validation;
use ScriptHoodie\Crud\Symfony\Validator;

$symfonyValidator = Validation::createValidatorBuilder()
    ->enableAttributeMapping()
    ->getValidator();

$validator = new Validator($symfonyValidator);

Create your entity classes with Symfony validation attributes:

use Symfony\Component\Validator\Constraints as Assert;

class Product
{
    #[Assert\NotBlank]
    #[Assert\Length(min: 2, max: 50)]
    public string $name;

    #[Assert\NotBlank]
    #[Assert\Positive]
    public float $price;

    #[Assert\NotBlank]
    #[Assert\Email]
    public string $email;
}

Validate your entities:

$product = new Product();
$product->name = "Example Product";
$product->price = 29.99;
$product->email = "user@example.com";

try {
    $validator->validate($product);
    // Entity is valid
    echo "Product is valid!";
} catch (\ScriptHoodie\Crud\Core\Exceptions\EntityValidationFailedException $e) {
    // Handle validation errors
    $errors = $e->getMessageBag();
    foreach ($errors as $field => $messages) {
        echo "Field {$field}: " . implode(', ', $messages) . "\n";
    }
}

Integration with ScriptHoodie CRUD Core

The validator seamlessly integrates with the ScriptHoodie CRUD operations. When creating a CRUD instance, you provide the validator as one of the dependencies:

use ScriptHoodie\Crud\Core\Crud;
use ScriptHoodie\Crud\Core\Hydrator;
use ScriptHoodie\Crud\Core\Persister;
use ScriptHoodie\Crud\Core\Reader;
use ScriptHoodie\Crud\Core\Factories\EntityFactory;
use ScriptHoodie\Crud\Symfony\Validator;
use Symfony\Component\Validator\Validation;

// Setup the Symfony validator with attribute mapping
$symfonyValidator = Validation::createValidatorBuilder()
    ->enableAttributeMapping()
    ->getValidator();

// Create the validator wrapper
$validator = new Validator($symfonyValidator);

// Create implementations for other dependencies
$entityFactory = new YourEntityFactory(); // Implements EntityFactory
$hydrator = new YourHydrator();           // Implements Hydrator
$persister = new YourPersister();         // Implements Persister
$reader = new YourReader();               // Implements Reader

// Create the CRUD instance with all dependencies
$crud = new Crud(
    $entityFactory,
    $hydrator,
    $validator,
    $persister,
    $reader
);

// The validator will automatically be called during create/update operations
// If validation fails, EntityValidationFailedException will be thrown
try {
    $id = $crud->create([
        'name' => 'Product Name',
        'price' => 29.99,
        'email' => 'user@example.com'
    ]);

    echo "Product created with ID: " . $id;
} catch (\ScriptHoodie\Crud\Core\Exceptions\EntityValidationFailedException $e) {
    // Handle validation errors specific to your application
    $errors = $e->getMessageBag();
    foreach ($errors as $field => $messages) {
        echo "Field {$field}: " . implode(', ', $messages) . "\n";
    }
}

The integration ensures that all entities are validated according to their Symfony validation attributes before any persistence operations occur:

  1. During create() operations, entities are validated after hydration but before persistence
  2. During update() operations, entities are validated after hydration but before persistence
  3. If validation fails, an EntityValidationFailedException is thrown and the operation is aborted
  4. Valid entities proceed through the normal CRUD workflow

This approach provides:

  • Automatic validation as part of your CRUD operations
  • Detailed error messages when validation fails
  • Consistent exception handling throughout your application
  • Flexibility to use the validator independently when needed