# Undefined index using YAML

**URL:** <https://forum.getkirby.com/t/undefined-index-using-yaml/10679>\
**Category:** Questions\
**Tags:** v2\
**Created:** [June 24, 2018, 8:19pm UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679 "2018-06-24T20:19:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![EconHettie](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@EconHettie](https://forum.getkirby.com/u/EconHettie)\
**Post date:** [June 24, 2018, 8:19pm UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/1 "2018-06-24T20:19:20Z")

</div>

I want to add a class to an element based on a checkbox field. The checkbox field `checksound` is nested inside an [Engineer field](https://github.com/jenstornell/field-engineer) and therefore I have used YAML. However, I am getting the error:

> Undefined index: checksound

From the following code:

```
<?php foreach($project->extvideotype()->yaml() as $extvideo):?>
    <video class="player 
       <?php if ($extvideo['checksound'] == 'true'): ?>
           sound<?php endif; ?>
       ?>" playsinline controls loop>
       <source src="<?= $extvideosource->url(); ?>" type="<?= $extvideosource->mime() ?>">
    </video>

<?php endforeach ?>

```

This is odd as the output of `var_dump($project->extvideotype());` produces the following:

```
object(Field)#1140 (3) {
  ["page"]=>
  string(23) "projects/a-sum-of-parts"
  ["key"]=>
  string(12) "extvideotype"
  ["value"]=>
  string(109) "-
  extvideosource: "interstitials.mp4"
  vimeolink: "https://vimeo.com/channels/staffpicks/274716344"
  checksound: true"
}

```

`checksound` does exist so where am I going wrong? I have included the blueprint for context below. Any help is very much appreciated.

excerpt of project.yml

```
fields:
  videotype:
    label: Video Type
    type: fieldtoggle
    options:
      extvideo: External Video
      webvideo: Website Video
    show:
      extvideo: extvideotype
      webvideo: webvideolink websitelink
    hide:
      extvideo: webvideolink websitelink
      webvideo: extvideotype

  extvideotype:
    type: engineer
    fields:
      extvideosource:
        label: Self Video Source
        type: select
        options: videos
        width: 1/2

      vimeolink:
        label: Vimeo Link
        type: embed
        width: 1/2
      
      checksound:
        label: Sound
        type: checkbox

```

---

<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:** [June 24, 2018, 8:49pm UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/2 "2018-06-24T20:49:20Z")

</div>

Would be more interesting to see a dump of $extvideo

```auto
dump($extvideo);

```

---

<div class="post-metadata">

**Author:** ![EconHettie](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@EconHettie](https://forum.getkirby.com/u/EconHettie)\
**Post date:** [June 25, 2018, 7:21am UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/3 "2018-06-25T07:21:37Z")

</div>

Ah yes. The output is

```
array(3) {
  ["extvideosource"]=>
  string(22) "interstitials.mp4"
  ["vimeolink"]=>
  string(27) "https://vimeo.com/channels/staffpicks/274716344"
  ["checksound"]=>
  bool(true)
}
```

---

<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:** [June 25, 2018, 8:00am UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/4 "2018-06-25T08:00:34Z")

</div>

Hm, that’s weird. And the error message definitely refers to this piece of the code?

---

<div class="post-metadata">

**Author:** ![EconHettie](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@EconHettie](https://forum.getkirby.com/u/EconHettie)\
**Post date:** [June 25, 2018, 10:01am UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/5 "2018-06-25T10:01:13Z")

</div>

I realised that perhaps the problem is that there is no `checksound` index for the other “projects”. I would have thought that checkbox would be output and set to false?

```
array(2) {
  ["extvideosource"]=>
  string(18) "introduction.mp4"
  ["vimeolink"]=>
  string(0) ""
}

```

Another:

```
array(2) {
  ["extvideosource"]=>
  string(15) "draw_record.mp4"
  ["vimeolink"]=>
  string(0) ""
}
```

---

<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:** [June 25, 2018, 10:06am UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/6 "2018-06-25T10:06:55Z")

</div>

Usually I would expect this to create an empty field. Anyway, instead of using yaml() you could use `toStructure()` and then shouldn’t run into this issue. Or check if the index. exists first.

---

<div class="post-metadata">

**Author:** ![EconHettie](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@EconHettie](https://forum.getkirby.com/u/EconHettie)\
**Post date:** [June 25, 2018, 11:01am UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/7 "2018-06-25T11:01:18Z")

</div>

I used the toStructure field and it works thank you.

```
<?php foreach($project->extvideotype()->toStructure() as $extvideo):?>
        <?php if($extvideo->extvideosource()->isNotEmpty()): ?>
            <video class="player <?php if($extvideo->checksound()->bool()): ?>sound<?php endif; ?>" playsinline controls loop>
                <source src="<?= $extvideo->extvideosource()->toFile()->url(); ?>" type="<?= $extvideo->extvideosource()->toFile()->mime() ?>">
            </video>
        <?php endif ?>

<?php endforeach ?>
```

---

<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:27pm UTC](https://forum.getkirby.com/t/undefined-index-using-yaml/10679/8 "2025-02-06T17:27:56Z")

</div>

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


