Skip to content

4.x: more consistent validate callback #12697

@dereuromark

Description

@dereuromark

This is a (multiple allowed):

  • feature-discussion (RFC)

  • CakePHP Version: 4

What you did

        ->add('pr', 'unique-issue', [
            'rule' => ['validateUniqueIssue', ['scope' => ['repository']],
            'message' => 'This PR is already linked to a group with this issue.',
            'provider' => 'table',
        ]);

and

/**
 * @param string $value
 * @param array $additional
 * @param array $context
 *
 * @return bool
 */
public function validateUniqueIssue($value, array $additional, array $context)

What happened

This is fine until you remove the additional scope

        ->add('pr', 'unique-issue', [
            'rule' => ['validateUniqueIssue'],
            'message' => 'This PR is already linked to a group with this issue.',
            'provider' => 'table',
        ]);

Then it breaks as the arguments into the method fail with "ArgumentCountError: Too few arguments to function ...".
It now suddenly expects

/**
 * @param string $value
 * @param array $context
 *
 * @return bool
 */
public function validateUniqueIssue($value, array $context)

What you expected to happen

To have a more reliable and reusable kind of signature here, consistent in both cases so if used in one table validator with and in another without it still works out.

Either we keep 2 args and merge the additional keys into the existing context.
Or we always have 3 and the 2nd can be an empty array here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions