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

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

@mycarrysun

Description

@mycarrysun

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions