0

To set the maximum number of tag fields that can be added to a tag field, we created a custom module for validation.
I used this as a reference, but it seems to be done a little differently in craft5. https://github.com/craftcms/cms/issues/976

The source code I implemented is below.
TagField was not countable, it maybe not ArrayValidator...

private function attachEventHandlers(): void
{
    Event::on(Entry::class, Entry::EVENT_DEFINE_RULES, function(DefineRulesEvent $e) {
        $entry = $e->sender;

        if ($entry->section->handle == 'mysection') {
            $e->rules[] = [
                "field:keywords", 
                ArrayValidator::class, 
                'count' => [0,5],
                'on' => Entry::SCENARIO_LIVE,
                'message' => 'You can set up to 5 keywords.'
            ];
        }else{
            return;
        }
    });
}

Please, let me know your best practice.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.