Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Nested validation attribute default to first layer in translation file #1771

Open
mycarrysun opened this issue Aug 12, 2019 · 2 comments
Open

Comments

@mycarrysun
Copy link

When you need to use the same name for a field with the same slug in multiple validators that each used different nested keys, there is no way to define this in the translation file.

What you would need to do in the translation file is this:

return [
  //...
  'attributes' => [
    'unit_no' => 'Unit #',
    'vehicle.unit_no' => 'Unit #',
    'vehicles.*.unit_no' => 'Unit #',
  ]
];

Instead I just want to have this:

return [
  //...
  'attributes' => [
    'unit_no' => 'Unit #',
  ]
];

In order to do this we can change Illuminate\Validations\Concerns\FormatsMessages method getDisplayableAttribute() to do this:

/**
 * Get the displayable name of the attribute.
 *
 * @param  string  $attribute
 * @return string
 */
public function getDisplayableAttribute($attribute)
{
    $primaryAttribute = $this->getPrimaryAttribute($attribute);

    $expectedAttributes = $attribute != $primaryAttribute
                ? [$attribute, $primaryAttribute] : [$attribute];

    foreach ($expectedAttributes as $name) {


        // The developer may dynamically specify the array of custom attributes on this
        // validator instance. If the attribute exists in this array it is used over
        // the other ways of pulling the attribute name for this given attributes.
        if (isset($this->customAttributes[$name])) {
            return $this->customAttributes[$name];
        }

        // We allow for a developer to specify language lines for any attribute in this
        // application, which allows flexibility for displaying a unique displayable
        // version of the attribute name instead of the name used in an HTTP POST.
        if ($line = $this->getAttributeFromTranslations($name)) {
            return $line;
        }

        // this is the change right here, we can change the name key
        // and then retry getting the line from the translations file
        $parts = explode('.', $name);
        $name = array_pop($parts);

        if ($line = $this->getAttributeFromTranslations($name)) {
            return $line;
        }
        // end change
    }

    // When no language line has been specified for the attribute and it is also
    // an implicit attribute we will display the raw attribute's name and not
    // modify it with any of these replacements before we display the name.
    if (isset($this->implicitAttributes[$primaryAttribute])) {
        return $attribute;
    }

    return str_replace('_', ' ', Str::snake($attribute));
}

I can provide a PR if this is acceptable. Thinking it may also be good to make it an opt-in only type of thing, where when you create a validator, we will store the property $nestedAttributeFallback as false by default, but you could turn it on for this functionality.

@vpratfr
Copy link

vpratfr commented Mar 12, 2021

That would be very useful to us as we have lots of duplicated messages for similar attributes.

If not desireable in the framework, is there any way to define our own logic to fetch the translations and hook into the validator?

@vpratfr
Copy link

vpratfr commented Mar 12, 2021

Here is a PR laravel/framework#36587

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants