Jump to content

How to display messages next to fields in the site editor based on content of other fields


nurkka
 Share

Recommended Posts

Hi all!

I don't know where to start right now, so I would be grateful for any tips: I need to add a warning message to the first field of a RepeaterMatrix element, indicating when at least one specific other field within the RepeaterMatrix element contains a value. I think the task is not specific to Pro Fields, so I posted it here in general support.

Once this first message works, the second task would be to display a warning next to each of the combo field subfields, if the parent field contains a value.

In other words, I would need the ability to display warning messages next to fields in the page editor based on the value of other fields.

Does anyone know where to start implementing this?
Is it only possible with JavaScript or could there be any PW features to do it?

Link to comment
Share on other sites

You can do something like this:

$wire->addHookBefore('Inputfield::render', function(HookEvent $event) {
	/** @var Inputfield $inputfield */
	$inputfield = $event->object;
	$process = $this->wire()->process;
	// Return early if this is not ProcessPageEdit
	if(!$process instanceof ProcessPageEdit) return;
	// The page being edited
	$page = $process->getPage();
	// The field associated with the inputfield, if any
	// Useful for when the inputfield is in a repeater, as the inputfield name will have a varying suffix
	$field = $inputfield->hasField;
	// The page that the inputfield belongs to
	// Useful for identifying if the inputfield is in a repeater
	$inputfield_page = $inputfield->hasPage;

	// Return early if this is not a page we are targeting
	if($page->template != 'test_combo') return;

	// Do some check to identify the inputfield by name or field name
	if($field && $field->name === 'text_1' && $inputfield_page->template == 'repeater_test_repeater') {
		// Check some other field value if the message depends on it
		if($page->test_combo->my_date === '2024-10-18 00:00:00') {
			// Show an error message
			$inputfield->error('This is a test error message');
		}
	}

	// Do some check to identify the inputfield by name or field name
	if($inputfield->name === 'test_combo_my_date') {
		// Check some other field value if the message depends on it
		if($page->test_repeater->first()->text_1 === 'hello') {
			// Show an error message
			$inputfield->error('Another test error message');
		}
	}
});

image.png.bd077f035ca09ed9489c1d9b3c63625e.png

 

 

Edited by Robin S
Expanded the example
  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...