Laravel validation attributes “nice names”

后端 未结 10 1735
[愿得一人]
[愿得一人] 2020-12-02 05:41

I\'m trying to use the validation attributes in \"language > {language} > validation.php\", to replace the :attribute name (input name) for a proper to read name (example: f

相关标签:
10条回答
  • 2020-12-02 06:25

    Well, it's quite an old question but I few I should point that problem of having the language appearing at the URL can be solved by:

    • Changing the language and fallback_language at config/app.php;
    • or by setting \App::setLocale($lang)

    If needed to persist it through session, I currently use the "AppServiceProvider" to do this, but, I think a middleware might be a better approach if changing the language by URL is needed, so, I do like this at my provider:

        if(!Session::has('locale'))
        {
            $locale = MyLocaleService::whatLocaleShouldIUse();
            Session::put('locale', $locale);
        }
    
        \App::setLocale(Session::get('locale'));
    

    This way I handle the session and it don't stick at my url.

    To clarify I'm currently using Laravel 5.1+ but shouldn't be a different behavior from 4.x;

    0 讨论(0)
  • 2020-12-02 06:30
    $customAttributes = [
    'email' => 'email address',
    ];
    
    $validator = Validator::make($input, $rules, $messages, $customAttributes);
    
    0 讨论(0)
  • 2020-12-02 06:36

    In the "attributes" array the key is the input name and the value is the string you want to show in the message.

    An example if you have an input like this

     <input id="first-name" name="first-name" type="text" value="">
    

    The array (in the validation.php file) should be

     'attributes' => array(
        'first-name' => 'Voornaam'),
    

    I tried the same thing and it works great. Hope this helps.

    EDIT

    Also I am noticing you don't pass a parameter to $errors->has() so maybe that's the problem.

    To fix this check out in the controller if you have a code like this

    return Redirect::route('page')->withErrors(array('register' => $validator));
    

    then you have to pass to the has() method the "register" key (or whatever you are using) like this

    @if($errors->has('register')
    .... //code here
    @endif
    

    Another way to display error messages is the following one which I prefer (I use Twitter Bootstrap for the design but of course you can change those with your own design)

     @if (isset($errors) and count($errors->all()) > 0)
     <div class="alert alert-error">
        <h4 class="alert-heading">Problem!</h4>
         <ul>
            @foreach ($errors->all('<li>:message</li>') as $message)
             {{ $message }}
           @endforeach
        </ul>
    </div>
    
    0 讨论(0)
  • 2020-12-02 06:36

    In Laravel 4.1 the easy way to do this is go to the lang folder -> your language(default en) -> validation.php.

    When you have this in your model, for example:

    'group_id' => 'Integer|required',
    'adult_id' => 'Integer|required',
    

    And you do not want the error to be "please enter a group id", you can create "nice" validation messages by adding a custom array in validation.php. So in our example, the custom array would look like this:

    'custom' => array(
        'adult_id' => array(
            'required' => 'Please choose some parents!',
        ),
        'group_id' => array(
            'required' => 'Please choose a group or choose temp!',
        ),
    ),
    

    This also works with multi-language apps, you just need to edit (create) the correct language validation file.

    The default language is stored in the app/config/app.php configuration file, and is English by default. This can be changed at any time using the App::setLocale method.

    More info to both errors and languages can be found here validation and localization.

    0 讨论(0)
提交回复
热议问题