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

Enable translating field attributes when validating fields. #2764

Merged

Conversation

jwahdatehagh
Copy link
Contributor

@jwahdatehagh jwahdatehagh commented Oct 29, 2020

This fixes the issue of not being able to translate attribute names when validating forms.

It enables utilizing Laravels custom validation rules for form validations without changing previous behaviour.

You can now specify custom attribute names and messages by adding configuration for the fields' handle in /resources/lang/{locale}/validation.php:

<?php

return [
    // ...

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Language Lines
    |--------------------------------------------------------------------------
    |
    | Here you may specify custom validation messages for attributes using the
    | convention "attribute.rule" to name the lines. This makes it quick to
    | specify a specific custom language line for a given attribute rule.
    |
    */

    'custom' => [
        'accept_privacy_policy' => [
            'required' => 'Please accept the privacy policy.',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Custom Validation Attributes
    |--------------------------------------------------------------------------
    |
    | The following language lines are used to swap our attribute placeholder
    | with something more reader friendly such as "E-Mail Address" instead
    | of "email". This simply helps us make our message more expressive.
    |
    */

    'attributes' => [
        'accept_privacy_policy' => 'privacy policy',
    ],
];

If a custom validation attribute or message doesn't exist, it falls back to the current behaviour of displaying the configured field display name.

I only found this issue from Statamic 2: statamic/v2-hub#1135

@jasonvarga
Copy link
Member

Wonderful, thank you for this.

@jasonvarga jasonvarga merged commit 94a980e into statamic:master Nov 5, 2020
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

Successfully merging this pull request may close these issues.

2 participants