Jump to content

Field dependency on field within repeater based on field outside of repeater?


999design
 Share

Recommended Posts

Hello

I've just hit a scenario where I have a particular field (within a repeater) that I'd like to set up a field dependency for, so that it shows only if an option from a page reference field (that sits outside of the repeater) is selected.

Simply using the fieldname=value approach doesn't seem to work. I'm assuming that this is because the field in question is inside a repeater and can't access information from the fields that sit outside of it?

I tried using the following without luck. To be honest I was just guessing at this point.

  • page.fieldname=value
  • forpage.fieldname=value

There's other ways that I can set up my template, but I was just curious if there was missing a trick somewhere?

Link to comment
Share on other sites

37 minutes ago, 999design said:

@MarkE Thanks

I think that would work within a frontend template but I don't think I can write it like that in the "Show if" dependency field settings. It seems a little bit more limited.

There may be some ideas here 

 

  • Like 1
Link to comment
Share on other sites

  • 9 months later...

@999design - I just found myself wanting this exact functionality. The easiest way for now seems to be Robin's https://processwire.com/modules/custom-inputfield-dependencies/. The custom PHP option does the trick with something like this:

$p = $page->getForPage();
if($p->field_name == 'value') return true;  
return false;

The caveat is that the conditions provided by this module are only applied on page load, not instantly via JS like the PW core feature. But definitely better than nothing for now.

  • Like 2
Link to comment
Share on other sites

I am thinking that this might actually be an easy fix in the PW core.

I just did a quick edit, replacing this line: https://github.com/processwire/processwire/blob/cc43f8e67600fc3ad73ea3ac095d67f166c5ca96/wire/templates-admin/scripts/inputfields.js#L1483 with:

var field = match[1];
if(field.indexOf('forpage.') == 0) {
	field = field.replace('forpage.', '').replace(/\_repeater\d+/g, '');
}

This lets me use: 

forpage.fieldname=value 

as described by @999design in the OP.

@ryan - can you see any problems with this approach? Maybe I've overlooked something?

Note that this approach currently won't work with AJAX loaded repeater items.

  • Like 6
  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...

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

×
×
  • Create New...