Robin S

Require a specific number of images in a field

Recommended Posts

Suppose you have an images field and you want editors to upload a specific number of images to that field.

Using a hook in /site/ready.php you can display a field error in Page Edit if the number of images in the field does not match the required number. Just like the standard "required" behaviour, the requirement does not prevent the field being saved if the number of images is not correct so you would still want to check the image count in your template.

$wire->addHookAfter('InputfieldImage::processInput', function(HookEvent $event) {
    $inputfield = $event->object;
    // Only for this field
    if($inputfield->hasField != 'images') return;
    // Only in ProcessPageEdit
    if($this->process != 'ProcessPageEdit') return;
    $page = $this->process->getPage();
    // Only for this template
    if($page->template == 'home') {
        if(count($inputfield->value) !== 4) $inputfield->error("Please upload exactly 4 images to this field");
    }
});

 

  • Like 7

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.