Skip to content

Named arguments support in Recaptcha2 constraint #69

@onEXHovia

Description

@onEXHovia

In Symfony 7.4, support for the options parameter in Validator constraints was deprecated in favor of named arguments.
symfony/symfony#60801

Projects that currently use

new Recaptcha2(['message' => 'recaptcha_invalid']);

cannot safely upgrade to Symfony 8.0, even though Symfony 8 support is declared in this bundle.

At the same time, switching to

new Recaptcha2(message: 'recaptcha_invalid');

currently fails, because Recaptcha2 does not define its own constructor with a message argument.

A minimal fix would be to add an explicit constructor to the constraint

use Symfony\Component\Validator\Constraint;

final class Recaptcha2 extends Constraint
{
    public string $message = 'Invalid ReCaptcha.';

    public function __construct(
        ?string $message = null,
        ?array $groups = null,
        mixed $payload = null,
    ) {
        parent::__construct(null, $groups, $payload);

        $this->message = $message ?? $this->message;
    }

    public function validatedBy(): string
    {
        return 'recaptcha2';
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions