steevanb/symfony-validator-constraints

添加验证约束

此包的规范仓库似乎已消失,因此该包已被冻结。

1.0.1 2016-08-26 09:17 UTC

This package is auto-updated.

Last update: 2023-04-28 08:50:16 UTC


README

版本 ![symfony](https://img.shields.io/badge/symfony/validator-^2.3 || ^3.0-blue.svg) ![行数](https://img.shields.io/badge/code lines-314-green.svg) 总下载量 SensionLabsInsight Scrutinizer

symfony-validator-constraints

symfony/validators 添加约束。

变更日志

UniqueObject

UniqueEntity 断言新实体不在数据库中。

但如果您要断言一个对象在数组或 \Traversable 中是唯一的,而不访问数据库,则可以使用 UniqueObject。

# Resources/config/validation/Bar.yml
Foo\Bar:
    properties:
        baz:
            - steevanb\SymfonyValidatorConstraints\Constraints\UniqueObject:
                # uniq id to stop error validation when error found,
                # otherwise you will have an error by object in collection
                uniqid: bar_baz
                # properties and getters to generate an "objet identifier",
                # who has to be unique
                properties: [foo, bar]
                getters: [getFoo(), getBar()]
                groups: [add]
                message: translation.message

示例

Foo\Bar:
    property:
        baz:
            - steevanb\SymfonyValidatorConstraints\Constraints\UniqueObject:
                uniqid: bar_baz
                properties: [foo]
                groups: [add]
$object1 = new Foo\Baz();
$object1->foo = 1;
$object1->bar = 2;

$object2 = new Foo\Baz();
$object2->foo = 1;
$object2->bar = 2;

$collection = new Foo\Bar();
$collection->add($object1);
$collection->add($object2);

// errors will contain 1 error, because $object1->foo and $object2->foo are identicals
$errors = $validator->validate($collection, null, ['add']);

// errors will not contain any error
$object1->foo = 2;
$errors = $validator->validate($collection, null, ['add']);