Jump to content

Preventing Duplicates with Repeater


Lauren
 Share

Recommended Posts

Hi there,

Is there is a built-in way to prevent duplicates when it comes to the Repeater field type?

The Repeater I have setup contains two fields: one is a select list of other Pages and the second is just a text field. Ideally, I'd love the select list of Pages to work similarly to how the Fields list in Admin works. As you're adding Fields to a Template, the ones you've already added become disabled in the list so they cannot be added again.

If that's not possible, then would something like the following be the next best solution as a module?

public function init() {
    $this->addHookAfter("InputfieldRepeater::processInput", $this, "validateNoDups");
}

public function validateNoDups($event){
    $field = $event->object;
    
    if($field->name == "technical_specs"){
        $page = $this->modules->ProcessPageEdit->getPage();
        $specs = $page->get("$field->name");
        $s = array();
        foreach($specs as $spec) {
            if ($spec->technical_spec) {
                $s[] = $spec->technical_spec->title;
            }
        }
        $dups = $this->get_duplicates($s);
        if ($dups) {
            $field->error("You have duplicate technical specs: " . implode(', ', $dups));
        }
    }
}
 
Thanks!
 
Lauren
Link to comment
Share on other sites

Oh, thank you! I didn't know there was a reply.

What you're suggesting doesn't quite work because the field to select the Page is contained within a Repeater.

So, here's my setup:

I have a collection of Technical Specs (Pages). Those Technical Specs are added to Products by way of a Repeater field type containing two fields: a Page select list of the Technical Specs (tech_spec) and a text field for the value (spec_value). So, for example, Product A has a Cure Time (tech_spec) of 1 hour (spec_value). I'm not sure how to do the inline image popup like you did, so I attached some screen shots.

Maybe instead of the Repeater there's another field I should be using to accomplish this?

Thanks for any insight you can provide. I'm fine with going the custom module route, but I always like to know if there's a native way to do it! The less code to maintain, the better! :)

post-2808-0-37248200-1419876100_thumb.pn

post-2808-0-77140000-1419876406_thumb.pn

Link to comment
Share on other sites

hi lauren,

don't know if it is working with repeater fields, sorry - but repeaters are out anyway ;) i think the PageTable is exactly what you are looking for and you should definitely try out this great field that opens tons of opportunities!

this topic is exactly for you: https://processwire.com/talk/topic/6866-page-field-in-pagetable/

in this image from the thread above you see repeaters (top) compared to pagetable (bottom):

3.png

see how much space you will save :) btw: looks like page field should work with repeaters?!

I'm not sure how to do the inline image popup like you did, so I attached some screen shots.

here you go

post-2137-0-97450900-1419878945_thumb.pn

Oh, thank you! I didn't know there was a reply.

you can automatically follow topics you replied to: https://processwire.com/talk/index.php?app=core&module=usercp&tab=core&area=notifications

post-2137-0-01441800-1419879003_thumb.pn

  • Like 5
Link to comment
Share on other sites

Oh geez! Sounds like I need to get with the times! ;P The PageTable field type sounds like the way to go.

Thank you SO much for being so helpful, not just in answering my question but also helping me navigate my way around the forum!

Thank you :)

  • Like 2
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

×
×
  • Create New...