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:
- During
create()operations, entities are validated after hydration but before persistence - During
update()operations, entities are validated after hydration but before persistence - If validation fails, an
EntityValidationFailedExceptionis thrown and the operation is aborted - 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
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2026-04-18