AtLeastOneOf example

$expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull()new Range(['min' => 3])]]));
        $expected->addPropertyConstraint('firstName', new Collection([
            'foo' => [new NotNull()new Range(['min' => 3])],
            'bar' => new Range(['min' => 5]),
            'baz' => new Required([new Email()]),
            'qux' => new Optional([new NotBlank()]),
        ], null, null, true));
        $expected->addPropertyConstraint('firstName', new Choice([
            'message' => 'Must be one of %choices%',
            'choices' => ['A', 'B'],
        ]));
        $expected->addPropertyConstraint('firstName', new AtLeastOneOf([
            new NotNull(),
            new Range(['min' => 3]),
        ], null, null, 'foo', null, false));
        $expected->addPropertyConstraint('firstName', new Sequentially([
            new NotBlank(),
            new Range(['min' => 5]),
        ]));
        $expected->addPropertyConstraint('childA', new Valid());
        $expected->addPropertyConstraint('childB', new Valid());
        $expected->addGetterConstraint('lastName', new NotNull());
        $expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
        
'foo' => [
                    new Assert\NotNull(),
                    new Assert\Range(min: 3),
                ],
                'bar' => new Assert\Range(min: 5),
                'baz' => new Assert\Required([new Assert\Email()]),
                'qux' => new Assert\Optional([new Assert\NotBlank()]),
            ],
            allowExtraFields: true
        ),
        Assert\Choice(choices: ['A', 'B'], message: 'Must be one of %choices%'),
        Assert\AtLeastOneOf(
            constraints: [
                new Assert\NotNull(),
                new Assert\Range(min: 3),
            ],
            message: 'foo',
            includeInternalMessages: false,
        ),
        Assert\Sequentially([
            new Assert\NotBlank(),
            new Assert\Range(min: 5),
        ]),
    ]

    protected function createValidator(): AtLeastOneOfValidator
    {
        return new AtLeastOneOfValidator();
    }

    /** * @dataProvider getValidCombinations */
    public function testValidCombinations($value$constraints)
    {
        $this->assertCount(0, Validation::createValidator()->validate($valuenew AtLeastOneOf($constraints)));
    }

    public static function getValidCombinations()
    {
        return [
            ['symfony', [
                new Length(['min' => 10]),
                new EqualTo(['value' => 'symfony']),
            ]],
            [150, [
                new Range(['min' => 10, 'max' => 20]),
                
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/** * @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl> */
class AtLeastOneOfTest extends TestCase
{
    public function testRejectNonConstraints()
    {
        $this->expectException(ConstraintDefinitionException::class);
        new AtLeastOneOf([
            'foo',
        ]);
    }

    public function testRejectValidConstraint()
    {
        $this->expectException(ConstraintDefinitionException::class);
        new AtLeastOneOf([
            new Valid(),
        ]);
    }
}
Home | Imprint | This part of the site doesn't use cookies.