Skip to content

Conversation

@onursahindur
Copy link
Contributor

No description provided.

@onursahindur onursahindur changed the title Support required() Undefined index 0 fix with Turkish localizations and required flag added Sep 3, 2025
Copy link
Member

@andreia andreia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @onursahindur :)

Thank you so much for taking the time to work on this, and apologies for the delay in getting back to you!

The "Undefined index 0" fix and the translations look great! 🎉

Regarding the required() option added to the Google Autocomplete select field: the select field itself is only used to search the Google API and populate the fields specified in the withFields() method. These are the fields where required() validation can be applied.

The select field isn’t saved to the database currently (as you can see with ->dehydrated(false) used here) only the fields provided in withFields() are persisted.

Do you have a specific use case where making the select field itself required would be helpful?

@onursahindur
Copy link
Contributor Author

onursahindur commented Sep 8, 2025

Regarding the required() option added to the Google Autocomplete select field: the select field itself is only used to search the Google API and populate the fields specified in the withFields() method. These are the fields where required() validation can be applied.

The select field isn’t saved to the database currently (as you can see with ->dehydrated(false) used here) only the fields provided in withFields() are persisted.

Do you have a specific use case where making the select field itself required would be helpful?

Hello, thanks!
In my current use case, I have two radio buttons that toggle different sets of fields. One option is location-based, and the other is not.
When the user selects the location-based option, the search field becomes visible and should be required. When the other option is selected, the search field is hidden and should not be required.

The fields under withFields are completely hidden since I don’t want to expose them to the user, I only need the search field to be visible, while the address, latitude, longitude, etc. are handled in the background.

That’s why having the required flag on the search field would be very helpful for my case.
In fact, there might be other scenarios where required is needed, so leaving this flexibility to the library user wouldn’t be harmful 😊

@andreia
Copy link
Member

andreia commented Sep 8, 2025

@onursahindur Ah, that makes perfect sense 🙂 Thank you for explaining your use case!

I’ll run some tests on my side just to double-check everything is working smoothly.

If you’d like, you could also create a separate PR for the ‘Undefined index 0’ fix and another one for the 'Turkish localizations' so I can merge those right away. That way, we can keep this PR focused just on the required option.

@andreia
Copy link
Member

andreia commented Sep 14, 2025

@onursahindur I was thinking about how to solve the issue when using ->required() on edit pages. If the address data already exists in the database and you are editing some other field, it should not keep requiring this field when it's not needed. So maybe change the getIsRequired() method to something like:

    public function getIsRequired(string $operation, ?Get $get = null): bool
    {
        // If the user hasn't explicitly set the field as required, return false
        if (!$this->evaluate($this->isRequired)) {
            return false;
        }

        // If this is an edit operation and we have access to form data
        if ($operation === 'edit' && $get !== null) {
            // Check if any of the child fields have values
            foreach ($this->getWithFields() as $field) {
                $fieldValue = $get($field->getName());

                // If any child field has a non-empty value, consider the requirement satisfied
                if (!empty($fieldValue)) {
                    return false;
                }
            }
        }

        // For create operations or when no child fields have values, return the original required state
        return $this->evaluate($this->isRequired);
    }

And change the ->required() method on Select component to:

    ->required(function (string $operation, Get $get) {
        return $this->getIsRequired($operation, $get);
    })

So:

Create operations:

  • The search field will be required only if explicitly call ->required() on the component.

Edit operations:

  • If you've called ->required() AND any of the child fields (address, latitude, longitude, etc.) have existing values from the database, the search field will NOT be required.
  • If you've called ->required() AND none of the child fields have values, the search field WILL be required.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants