Skip to content

About Validation Providers #8229

@mirko-pagliai

Description

@mirko-pagliai

I noticed that in terms of validation, there's no shortcut to perform a sort of OR between different rules (e.g., the field can be either date or datetime). And I can easily imagine that implementing something like this makes very little sense (so much implementation for a rarely used function).

Of course, it's possible to do so by implementing your own callback, I know.

However, I saw that the Validation Providers section is correctly included, and that it correctly mentions the table provider and the default provider.

So, might it be useful to mention that the validator is available for some methods, such as getProvider(), which can then also allow statically access to the provider's methods?

/** @var class-string<\Cake\Validation\Validation> $defaultProvider */
$defaultProvider = $validator->getProvider('default');

$validator
    ->add('start_on', 'dateOrDatetime', [
        'rule' => function ($value) use ($defaultProvider): bool {
            return $defaultProvider::datetime($value) || $defaultProvider::date($value);
        },
        'message' => __('It should be a date or a date and time'),
    ])

I ask a question before opening a PR because maybe it's stupid or useless, or because there's a better way that I haven't found. If it's okay, I can do a PR myself (this way I avoid always delegating my ideas/suggestions, when I can! :-))

p.s. In the same section, immediately after, the possibility of using callbacks is mentioned. But I don't see a close correlation with providers (that's the section). Is it normal for it to be there?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions