Create page from front end checkbox field

I am using the the front end form cookbook: https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend

I want have a field that is a checkbox, I thought I could do this:

<input type="checkbox" id="user" name="user" value="Yes" <?= $data['user'] === 'Yes' ? 'checked="checked"' : null ?>/>

But I get the error:

Trying to access array offset on value of type bool

Any ideas what this means?

Yes, you have to check if $data[‘user’] is set, because it evaluates to false when the form is loaded and there is no form data yet.

See the example form in that recipe.

is that not what <?= $data['user'] === 'Yes' ? 'checked="checked"' : null ?> is doing?

No, not at all, you try to compare $data[‘user’] to a value Yes, but you don’t check if $data[‘user’] is set.

Sorry, probably being really thick, but this is not solved. I can’t workout the correct way to check the data has been set and then map it to the checked value.

In php, we check if a variable is set using isset(), like in the cookbook, so you need to do two things: first check if isset, second if the value is the expected value (using &&)

isset( $data['user'] ) && $data['user'] === 'Yes' ? `'checked="checked"' : null`

AH! thanks sorry completely missed the honey pot field in the example. It’s working great now.