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

Bug: localised validation messages #2845

Closed
alex-misch opened this issue Apr 19, 2020 · 0 comments
Closed

Bug: localised validation messages #2845

alex-misch opened this issue Apr 19, 2020 · 0 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@alex-misch
Copy link

alex-misch commented Apr 19, 2020

Hello

I didn't find any way to localise field interpolation in validation messages in models. It looks like a bug. Sorry if I'm wrong. Now I wrote little workaround. I`ll leave it below but in my opinion its looks bad. In CodeIgniter v3 I've loaded two lang files in controller and {field} was replaced to localised value automatically.

Affected module(s)

  • Model
  • Validation

Expected behavior, and steps to reproduce if appropriate

File structure:

Language/en/Validation.php:

return ['valid_date' => 'Field {field} is invalid'];

Language/en/Entity.php:

return ['date' => 'My localised date label'];

Models/MyModel.php:

class MyModel extends Model {
	protected $validationRules = [
		"date_start" => [
			"rules" => "valid_date", "label" => "Entity.date"
		],
	];
}

Now call model validation in controller in controller:

class MyController extends Controller
{
	function index() {
		$model = model(\App\Models\MyModel::class);
		$model->insert(['date_start' => 'invalid date']);
		echo $model->getErrors();
	}
}

And you've got response

["date" => "Field Entity.date is invalid"];

So how to localise Entity.date field?

Workaround

class LocalisedModel extends Model
{
	public function __construct(...$args)
	{
		parent::__construct(...$args);
		$this->validationRules = array_map(function ($rule) {
			if (is_array($rule) && $rule['label']) {
				$rule['label'] = lang($rule['label']);
			}
			return $rule;
		}, $this->validationRules);
	}
}

Context

  • OS: CentOS
  • Web server Apache 2.4.43
  • PHP version 7.2
@alex-misch alex-misch added the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 19, 2020
@alex-misch alex-misch changed the title Bug: Bug: localised validation messages Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants