Jump to content

Hook to check for repeater items with same field values


Bike
 Share

Recommended Posts

Hi,

I am using this code to check if a repeater item, which has a couple of field, has one field with the same value multiple times.

It works for textfields or checkboxes but not for the dropdown which I need:

// Meldung bei doppelten Größen
$wire->addHookBefore('Pages::save', function (HookEvent $event) {
    $page = $event->arguments(0);
    // Ersetze 'your_repeater_field' durch den Namen deines Repeaters
    $repeaterFieldName = 'groessen';

    // Ersetze 'dropdown_field' durch den Namen deines Dropdown-Feldes im Repeater
    $dropdownFieldName = 'groesse';
    // Prüfen, ob die Seite das Repeater-Feld hat
    if (!$page->template->hasField($repeaterFieldName)) return;

    // Werte aus dem Repeater sammeln
    $selectedValues = [];
    foreach ($page->$repeaterFieldName as $item) {
        $selectedValue = $item->dropdownFieldName;

        if (in_array($selectedValue, $selectedValues)) {
            // Fehler hinzufügen und Speichern verhindern
            $this->wire('session')->warning("Diese Größe existiert bereits!");
            $event->replace = true;
            return;
        }

        $selectedValues[] = $selectedValue;
    }
});

Can't figure it out!

Thanks!

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...