Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach forms do not use relation field label in validation messages #6099

Closed
dmason30 opened this issue Dec 11, 2023 · 2 comments
Closed

Attach forms do not use relation field label in validation messages #6099

dmason30 opened this issue Dec 11, 2023 · 2 comments
Labels
bug Verified bug by the Nova team next
Milestone

Comments

@dmason30
Copy link

dmason30 commented Dec 11, 2023

  • Nova Version: 4.32.6

Description:

MorphToMany::make(__('Partners'), 'affiliates', AffiliateResource::class)
                ->searchable(),

It seems to be using a singular version of the relation name instead of the provided field label:
image

This becomes more of an issue for us when we support multiple languages for example below where the english field label is 'Affiliates' and lets assume another (fake) language this translates to 'Äffïlïätës'.

See below how the validation message is successfully translated also (using our fake language) but the :attribute part is stuck using the english singular version of the relation name.

image

Same seems to apply to any relations that supports attaching:

  • MorphToMany
  • BelongsToMany
  • etc

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. With the field above
  2. Submit select empty
  3. See that validation message is not using label.

Digging deeper

The issue seems to be ResourceAttachController line 82 is attempting to get an attribute label but at no point resolves the field label and it is passed on line 85 to the Validator property customAttributes via $this->customRulesKeys.

At a minimum it would be acceptable for it to use $resource::singularLabel() here if you are unable to access the field label. However, it seems that PerformsValidation::validationAttachableAttributeFor method is finding the field and then calling getValidationAttribute which does this:

    public function getValidationAttribute(NovaRequest $request)
    {
        return $this->validationAttribute ?? Str::singular($this->attribute);
    }

Fix

I can't see anywhere in the code where validationAttribute is or can be set? Maybe its dynamically done somewhere.
Anyway, I think as a minimum we should change this to fallback to using the name property before resorting to the attribute.

    public function getValidationAttribute(NovaRequest $request)
    {
        return $this->validationAttribute ?? $this->name ?? Str::singular($this->attribute);
    }

Result is the message uses the field label as expected:

image
@crynobone crynobone added the pending Issues that are pending triage label Feb 12, 2024
@crynobone crynobone added request Feature Request next and removed pending Issues that are pending triage labels Jul 31, 2024
@crynobone crynobone added this to the 5.x milestone Jul 31, 2024
@crynobone crynobone added bug Verified bug by the Nova team and removed request Feature Request labels Jul 31, 2024
@crynobone
Copy link
Member

Released with Laravel Nova 5.0.0

Feel free to open up a new issue if you're still experiencing this problem on the latest version.

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Verified bug by the Nova team next
Projects
None yet
Development

No branches or pull requests

2 participants