# Unique fields in structure?

**URL:** <https://forum.getkirby.com/t/unique-fields-in-structure/9937>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 10, 2018, 5:19pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937 "2018-03-10T17:19:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [March 10, 2018, 5:19pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/1 "2018-03-10T17:19:27Z")

</div>

Hello there,  
for a client I am having a structure field on a page which can contains entries in the following way:

- ID
- Name
- …

As the data is relatively simple, a structure field is working well. The problem is, that it’s possible to create multiple entries with the same ID (which is user-defined). What’s the best way to block saving when there are two or more items with the same ID value?

I’ve tried it with a hook and `panel->alert('Duplicate ID')` with the redirect coming after, but this still allows the saving process of the page. I’ve read about custom validators, but it seems that they require manual input in the blueprint, there’s no way of setting the page itself as value to validate.

So, what’s the best way for solving this problem?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 10, 2018, 6:06pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/2 "2018-03-10T18:06:12Z")

</div>

What is the purpose of the ID?

How about assigning a unique ID automatically? I created a plugin for that:

> **[texnixe/kirby-structure-id](https://github.com/texnixe/kirby-structure-id)**
>
> kirby-structure-id - Adds unique hashes to structure field entries

---

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [March 10, 2018, 6:21pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/3 "2018-03-10T18:21:07Z")

</div>

The purpose of the ID is the internal identifier. We have several bicycles in our company, many of them of the same type and size. To the visitor only the “master bike” is visible, but we need to manage them in the backend with their serial numbers, which are their ID. As this is given by the manufacturer, it can’t be created automatically.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 10, 2018, 6:25pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/4 "2018-03-10T18:25:12Z")

</div>

Then a custom validator would be the way to go that checks if the ID has already been used. But for this to work you’d probably have to set up a custom field that checks the state of the other structure fields on the fly via JS.

Otherwise, your validator could only ever check against what has already been saved to file.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 11, 2018, 8:38am UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/5 "2018-03-11T08:38:26Z")

</div>

I have thought about it some more and I think this could work:

```auto
v::$validators['unique'] = function($value, $field) {
  $values = array_column(yaml($value), $field);
  return count($values) === count(array_flip($values));
  // or
  // return count($values) !== count(array_unique($values));

};

```

Blueprint:

```nohighlight
fields:
  products:
    label: Products
    type: structure
    style: table
    validate:
      unique: id
    help: Make sure all ID fields contain unique numbers otherwise an error is thrown if you save the page.
    fields:
      id:
        label: ID
        type: number
        # etc.

```

Where `unique` is the name of the validator and `id` the name of the field you want to validate.

However, this solution would only validate when the page is saved, not when the user adds a new entry.

---

<div class="post-metadata">

**Author:** ![jlnostr](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jlnostr/32/4818_2.png) [@jlnostr](https://forum.getkirby.com/u/jlnostr)\
**Post date:** [March 11, 2018, 11:10am UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/6 "2018-03-11T11:10:11Z")

</div>

This code works as expected, it’s enough for me to notify the user when saving.  
I adapted it a bit to tell explicitely which entry is duplicated using the validator below:

```
v::$validators['unique'] = function($value, $field) {
	$values = array_column(yaml($value), $field);
	$ids = array();

	foreach ($values as $value) {
		if (in_array($value, $ids)) {
			$replace = array(
				'panel/' => "",
				url::home() => ""
			);
			panel()->alert('Doppelter Eintrag: '.$value);
			panel()->redirect(str_replace(array_keys($replace), array_values($replace), url::current()));
			return false;
		} else {
			$ids[] = $value;
		}
	}

	return true;
};

```

This works in my case, as I know that I won’t use the validator somewhere else. Thanks again for helping! 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:28pm UTC](https://forum.getkirby.com/t/unique-fields-in-structure/9937/7 "2025-02-06T17:28:39Z")

</div>

Archived on February 6, 2025, 5:28pm UTC


