nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

Attach forms do not use relation field label in validation messages

Open dmason30 opened this issue 1 year ago • 0 comments

  • 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

dmason30 avatar Dec 11 '23 11:12 dmason30