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 fieldif($inputfield->hasField !='images')return;// Only in ProcessPageEditif($this->process !='ProcessPageEdit')return;
$page = $this->process->getPage();// Only for this templateif($page->template=='home'){if(count($inputfield->value)!==4) $inputfield->error("Please upload exactly 4 images to this field");}});
Require a specific number of images in a field
in Tutorials
Posted
That was brilliant! thank you for this
That was brilliant, thank you for this