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

attributes nice names #72

Open
raulp opened this issue Aug 19, 2014 · 5 comments
Open

attributes nice names #72

raulp opened this issue Aug 19, 2014 · 5 comments

Comments

@raulp
Copy link

raulp commented Aug 19, 2014

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:

protected $validationAttributes = [
    'attr-name' => 'My Nice Attr Name',
];

Thank you

@dalabarge
Copy link
Contributor

@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 /lang/<locale>/models/<snake_case_name>.php could be used.

@dwightwatson
Copy link
Owner

Hey @raulp, got something like this setup in #66. You'll need to run on dev-develop for now as it will go into a 0.10.x release once a relevant PR is merged into Laravel.

@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.

@dwightwatson dwightwatson added this to the 0.10.x milestone Aug 19, 2014
@raulp
Copy link
Author

raulp commented Aug 20, 2014

hey guys.
i agree with @dalabarge that a separate file will be cool as well, but i think both ways should be implemented, as you might have only 1 language like most of the apps :)

@dwightwatson , sorry for opening an issue, but i could not found that while searching

@dalabarge
Copy link
Contributor

@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 $app->detectEnvironment() can accept an array or it can accept a closure or how the Observer classes work: I think having a getValidatingTranslator() method would be useful whereby you can resolve the preferred multi-lingual file-based translator or add an additional trait that overwrites it with the simplified model property-based translator.

The Translator should be interfaced so it's easy to make multiple adapters and it should basically return a class that has getters like getAttributeNames(), getCustomMessages(). I could then see having classes for a Post model like PostTranslator which is bolted onto the Post and looks something like:

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 Post model you'd have due to ValidatingTrait:

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();
}

@dwightwatson dwightwatson modified the milestones: 0.10.x, 0.11.x Sep 15, 2014
@dwightwatson dwightwatson removed this from the 0.11.x milestone Sep 23, 2014
@raulp raulp removed this from the 0.11.x milestone Sep 23, 2014
@Propaganistas
Copy link
Contributor

If I understand this discussion correctly, you are trying to reinvent the wheel.
The (now deprecated) Magniloquent package features niceNames supporting Laravel's translation services (through the use of trans()). Perhaps you could borrow some ideas to implement this feature?

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

No branches or pull requests

4 participants