-
Notifications
You must be signed in to change notification settings - Fork 76
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
attributes nice names #72
Comments
@dwightwatson I agree that having the messages and attributes would be helpful, though it doesn't play nicely when you deal with multi-language apps. Perhaps we should figure out a way to provide a file based language solution that integrates with the translation services of Laravel!? Perhaps those validation lines could be stored in paths dependent upon the model's namespace and then in the boot method or constructor method those arrays could be appended to the translation services. Something like |
Hey @raulp, got something like this setup in #66. You'll need to run on @dalabarge I quite like this idea, hadn't thought about localisation yet. I'll have a crack at this for the next release as it seems like I'll have a bit of time until the relevant PR is brought in. |
hey guys. @dwightwatson , sorry for opening an issue, but i could not found that while searching |
@raulp sure you could go for both but the way I'd handle that is by a translation manager being injected into the class. Sort of like how The class PostTranslator implements ValidatingTranslator{
protected $attributeNames = [
'foo' => 'Foo Bar',
];
protected $customMessages = [
'foo.bar' => "Foo must be a type of bar."
];
public function getAttributeNames()
{
return $this->atrributeNames ?: [];
}
public function getCustomMessages()
{
return $this->customMessages ?: [];
}
} This is the simplified version of what is proposed now. You could then have it localized with something like: class PostTranslator implements ValidatingTranslator{
protected $attributeNames = [
'en' => [
'foo' => 'Foo Bar',
],
];
protected $customMessages = [
'en' => [
'foo.bar' => "Foo must be a type of bar."
],
];
public function __construct(Config $config)
{
$this->config = $config;
}
public function getAttributeNames()
{
$locale = $this->getLocale();
return $this->atrributeNames[$locale] ?: [];
}
public function getCustomMessages()
{
$locale = $this->getLocale();
return $this->customMessages[$locale] ?: [];
}
public function getLocale()
{
return $this->config->get('app.locale', 'en');
}
} Obviously it should have more robustness by checking the local exists and/or falling back to the default locale, but as you can see this supports simplified translation and localized the existing methods but on a separate class. Then you could add the file based approached similarly like: class PostTranslator implements ValidatingTranslator, NamedValidatingTranslator{
protected $name;
public function __construct(Lang $lang)
{
$this->lang = $lang;
}
// Extra method from NamedValidatingTranslator
public function setName($name)
{
$this->name = $name;
}
public function getAttributeNames()
{
return $this->lang->get($this->name.'.validating.attributes');
}
public function getCustomMessages()
{
return $this->lang->get($this->name.'.validating.messages');
}
} Where on public function getValidatingTranslator()
{
if( ! isset($this->validatingTranslator) )
{
$this->validatingTranslator = new PostTranslator;
if( $this->validatingTranslator instanceof NamedValidatingTranslator)
{
$this->validatingTranslator->setName(get_class($this));
}
}
return $this->validatingTranslator;
}
public function getValidatingAttributeNames()
{
return $this->getValidatingTranslator()->getAttributeNames();
}
public function getValidatingCustomMessages()
{
return $this->getValidatingTranslator()->getCustomMessages();
} |
If I understand this discussion correctly, you are trying to reinvent the wheel. |
Hey
I think it will be useful if we would be able to add nice names for some attributes.
Laravel has a function to do that
setAttributeNames on a Validator instance
I'm thinking if we can define in the model something like:
Thank you
The text was updated successfully, but these errors were encountered: