scripthoodie/crud-symfony-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scripthoodie/crud-symfony-validator

最新稳定版本:0.3.0

Composer 安装命令:

composer require scripthoodie/crud-symfony-validator

包简介

Implementation for CRUD validator using Symfony validator.

README 文档

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

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2026-04-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固