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

Open
dmason30 opened this issue Dec 11, 2023 · 0 comments
Open

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

dmason30 opened this issue Dec 11, 2023 · 0 comments
Labels
pending Issues that are pending triage

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending Issues that are pending triage
Projects
None yet
Development

No branches or pull requests

2 participants