PawelGIX Posted February 28, 2024 Share Posted February 28, 2024 I have some custom fields and I wanted to add validation to them using hooks. Unfortunately, I cannot access the fields inside the images field. How to run custom validations on them and return an error when editing the page I want to add validation to aspect_ratio only when we have a given URL to the video I have this much for now but I don't know how to move on. <?php wire()->addHookAfter('ProcessPageEdit::processInput', null, 'checkAspectRatio'); function checkAspectRatio($event) { $form = $event->arguments(0); // ProcessPageEdit's processInput function may go recursive, so we want to skip // the instances where it does that by checking the second argument named "level" $level = $event->arguments(1); if($level > 0) return; if($form->get("additional_images")){ } } Link to comment Share on other sites More sharing options...
Robin S Posted February 28, 2024 Share Posted February 28, 2024 I was going to suggest using inputfield dependencies as this would be the simplest option but it turns out there's a problem when using these with custom image fields: https://github.com/processwire/processwire-issues/issues/1889 So here's a hook instead. In this demo I want to show an error message if text_2 is left empty when text_1 is populated. You can adapt the names/labels/logic to your case. $wire->addHookAfter('InputfieldText::processInput', function(HookEvent $event) { $inputfield = $event->object; $field = $inputfield->hasField; $page = $inputfield->hasPage; // Only for an inputfield associated with field "text_2" // Adjust the field name to suit if($field && $field->name === 'text_2') { // Only for an inputfield that is associated with a page using the custom image fields template // Adjust the template name to suit if($page && $page->template == 'field-images_custom') { /** @var InputfieldWrapper $wrapper */ $wrapper = $inputfield->parent; // Get sibling inputfield by label because the name will include a random string // Adjust inputfield label to suit $text_1 = $wrapper->children->get('label="Text 1"'); // Show an error message if text_2 is left empty when text_1 is populated if($text_1 && $text_1->value && !$inputfield->value) { $inputfield->error('A value for "Text 2" is required when "Text 1" is populated.'); } } } }); 2 1 Link to comment Share on other sites More sharing options...
PWaddict Posted February 18 Share Posted February 18 @Robin S Nice hook. I'm trying to unpublish the page if the custom field is empty but it's not working. if(!$inputfield->value) { $page->addStatus(Page::statusUnpublished); } Any idea how to accomplish that? Link to comment Share on other sites More sharing options...
Robin S Posted February 18 Share Posted February 18 @PWaddict, you would need to use a different hook for this because processInput is too early - it happens before Page Edit decides whether the page should be published so any changes to the published status made there would be overwritten. Something like this should work: $wire->addHookAfter('Pages::saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'test_custom_images') { $unpublish = false; foreach($page->images_custom as $image) { if(!$image->text_1) $unpublish = true; } if($unpublish) $page->addStatus(Page::statusUnpublished); } }); 1 Link to comment Share on other sites More sharing options...
PWaddict Posted February 18 Share Posted February 18 Thanks a lot @Robin S your code works great. I was trying on saveReady hook too using the below code but couldn't make it work properly. I wasn't using foreach loop as the image field I was testing it accepts only 1 image. $wire->addHookAfter('Pages::saveReady', function($event) { $page = $event->object->getPage(); if ($page->template->name != "my-page-template") return; if($page->image->custom_image_field == "") { $page->addStatus(Page::statusUnpublished); } }); 1 Link to comment Share on other sites More sharing options...
Robin S Posted February 18 Share Posted February 18 26 minutes ago, PWaddict said: I wasn't using foreach loop as the image field I was testing it accepts only 1 image. In saveReady the page is just about to be saved and therefore output formatting is off, so all image fields (including single image fields) will be a Pageimages array. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now