How to set custom attribute labels for nested inputs in Laravel

I am trying to get custom tags in error messages. My inputs are nested and I can check them using dot notation (e.g. POLICY.HOLDER.NAME ). They must be nested and all the caps so that they match the db structure, so I can easily assign them to the masses.

Using the same notation in 'attributes' => ['POLICY.HOLDER.NAME' => 'Policy Holder'] in resources/lang/en/validation.php failed.

I'm just trying to get them to match the labels that I set on the form.

app / Http / Controllers / OfferController.php (only the interesting part)

 public function postOffer(OfferRequest $request) { // $post_data = $request->all(); if (isset($post_data['POLICY'])) { // code to get data from $_POST and assign to models } } 

application / Http / Requests / OfferRequest.php

 <?php namespace App\Http\Requests; use App\Http\Requests\Request, Auth; class OfertaRequest extends Request { public function authorize() { return Auth::check(); } public function rules() { return [ 'POLICY.HOLDER.NAME' => 'required', ]; } public function forbiddenResponse() { return Response::make('Permission denied foo!', 403); } } 

resources / languages ​​/ EN / validation.php

 'attributes' => [ 'POLICY.HOLDER.NAME' => 'Some custom string here...', ], 

As you can see, I tried adding the input name to the array of custom validation attributes without success. Here is the error message that I get when I exit blank input:

. holde r. n field m e.

Pay attention to spaces. I tried it too, it didn't work.

+5
source share
1 answer

Explicitly declare it as nested arrays:

 'attributes' => [ 'POLICY' => [ 'HOLDER' => [ 'NAME' => 'Some custom string here...', ] ] ], 
+6
source

Source: https://habr.com/ru/post/1232184/


All Articles