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

[1.x] Implement Support for Translatable Validation Attribute Errors #4070

Open
wants to merge 4 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,23 @@ flarum-extension-manager:

why_not_modal:
title: Why Won't it Update

validation:
attributes:
minimum_stability: minimum stability
repositories: repositories
repositories.*: repositories
repositories.*.type: repository type
repositories.*.url: repository URL
extension_id: extension ID
update_mode: update mode
package: package
version: version
github_oauth: GitHub OAuth
github_oauth.*: GitHub OAuth
gitlab_oauth: GitLab OAuth
gitlab_oauth.*: GitLab OAuth
gitlab_token: GitLab Token
gitlab_token.*: GitLab Token
bearer: HTTP Bearer
bearer.*: HTTP Bearer
4 changes: 4 additions & 0 deletions extensions/suspend/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ flarum-suspend:
You have been unsuspended. You can head back to the forum by clicking on the following link:
{forum_url}
validation:
attributes:
suspendedUntil: suspended until
10 changes: 10 additions & 0 deletions extensions/tags/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,13 @@ flarum-tags:
choose_tags_placeholder: "{count, plural, one {Choose 1 more tag} other {Choose # more tags}}"
name: Name
tags: Tags

validation:
attributes:
name: name
slug: slug
is_hidden: hidden
description: description
color: color
tag_count_primary: => validation.attributes.tag_count_primary
tag_count_secondary: => validation.attributes.tag_count_secondary
1 change: 1 addition & 0 deletions framework/core/locale/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ validation:
present: "The :attribute field must be present."
regex: "The :attribute format is invalid."
required: "The :attribute field is required."
required_array_keys: "The :attribute array must contain entries for: :values."
required_if: "The :attribute field is required when :other is :value."
required_unless: "The :attribute field is required unless :other is in :values."
required_with: "The :attribute field is required when :values is present."
Expand Down
13 changes: 13 additions & 0 deletions framework/core/src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ public function getTitle()
return $this->composerJsonAttribute('extra.flarum-extension.title');
}

/**
* @return string|null
*/
public function getNamespace(): ?string
{
return Collection::make($this->composerJsonAttribute('autoload.psr-4'))
->filter(function ($source) {
return $source === 'src/';
})
->keys()
->first();
}

/**
* @return string
*/
Expand Down
19 changes: 19 additions & 0 deletions framework/core/src/Foundation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

abstract class AbstractValidator
{
use ExtensionIdTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -81,6 +83,22 @@ protected function getMessages()
return [];
}

/**
* @return array
*/
protected function getAttributeNames()
{
$extId = $this->getClassExtensionId();
$attributeNames = [];

foreach (array_keys($this->rules) as $attribute) {
$key = $extId ? "$extId.validation.attributes.$attribute" : "validation.attributes.$attribute";
$attributeNames[$attribute] = $this->translator->trans($key);
}

return $attributeNames;
}

/**
* Make a new validator instance for this model.
*
Expand All @@ -92,6 +110,7 @@ protected function makeValidator(array $attributes)
$rules = Arr::only($this->getRules(), array_keys($attributes));

$validator = $this->validator->make($attributes, $rules, $this->getMessages());
$validator->setAttributeNames($this->getAttributeNames());

foreach ($this->configuration as $callable) {
$callable($this, $validator);
Expand Down
31 changes: 31 additions & 0 deletions framework/core/src/Foundation/ExtensionIdTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Foundation;

use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;

trait ExtensionIdTrait
{
protected function getClassExtensionId(): ?string
{
$extensions = resolve(ExtensionManager::class);

return $extensions->getExtensions()
->mapWithKeys(function (Extension $extension) {
return [$extension->getId() => $extension->getNamespace()];
})
->filter(function ($namespace) {
return $namespace && str_starts_with(static::class, $namespace);
})
->keys()
->first();
}
}
Loading