# Require selection of one attribute between two during listing creation

**URL:** <https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039>\
**Category:** HivePress\
**Tags:** code\
**Created:** [May 28, 2024, 11:03pm UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039 "2024-05-28T23:03:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajfgl](https://community.hivepress.io/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@ajfgl](https://community.hivepress.io/u/ajfgl)\
**Post date:** [May 28, 2024, 11:03pm UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039/1 "2024-05-28T23:03:42Z")

</div>

I’m trying to require sellers to select from one attribute dropdown when creating a listing, but not allow selection of both:

```auto
add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        $listing = $form->get_model();

        // Attempt to retrieve the values using direct property access
        $service_1 = $listing->service_1;
        $service_2 = $listing->service_2;

        // Check if both fields are filled
        if ($service_1 && $service_2) {
            $errors[] = 'Please only fill out either "Service 1" or "Service 2", but not both.';
        }

        // Check if both fields are empty
        if (!$service_1 && !$service_2) {
            $errors[] = 'Please fill out one of the fields "Service 1" or "Service 2".';
        }

        return $errors;
    },
    100,
    2
);

```

but I can’t seem to get it to work. I’m trying to check if both are selected, then send an error message and then check if neither are selected, then send an error message.

I’m at a loss.

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [May 29, 2024, 11:58am UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039/3 "2024-05-29T11:58:40Z")

</div>

If you know the field name for sure, please try fetching values this way:

`$service_1 = $listing->get_service_1();`

The rest of the code snippet seems to be ok.

---

<div class="post-metadata">

**Author:** ![ajfgl](https://community.hivepress.io/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@ajfgl](https://community.hivepress.io/u/ajfgl)\
**Post date:** [May 29, 2024, 3:15pm UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039/4 "2024-05-29T15:15:32Z")

</div>

Still having an issue. These fields are select dropdowns. Would that change the necessary value retrieval method? I’m trying to check if any of the options has been selected within these dropdowns or not

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [May 30, 2024, 11:49am UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039/6 "2024-05-30T11:49:01Z")

</div>

If these are Select fields and the field names are correct, you can check if the field is set this way:

```auto
if($listing->get_service_1__id()) {
// it's set
}

```

Please notice the double underscore. If it still doesn’t work, you can debug this by using `var_dump` and check if the `$listing` object is not empty, if the function fires at all, and if so you can check the `$listing->get_service_1__id()` value.

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [June 29, 2024, 11:49am UTC](https://community.hivepress.io/t/require-selection-of-one-attribute-between-two-during-listing-creation/11039/7 "2024-06-29T11:49:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
