Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom attribute labels for nested inputs in Laravel

I'm trying to get custom labels in the error messages. My inputs are nested and i am able to validate them using dot notation (e.g. POLICY.HOLDER.NAME ). They have to be nested and all caps to match the db structure so I can easily mass assign them.

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

I'm just trying to get them to match the labels i've set in the form.

app/Http/Controllers/OfferController.php (just 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
    }
}

app/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/lang/en/validation.php

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

As you can see i've tried adding the input name in the Custom Validation Attributes array with no success. Here's the error message i get when leaving the input blank:

The p o l i c y. h o l d e r. n a m e field is required.

Note the spaces. I've tried that too, it didn't work.

like image 594
Andrei C Avatar asked Sep 24 '15 12:09

Andrei C


1 Answers

Explicitly declare it as nested arrays:

'attributes' => [
    'POLICY' => [
        'HOLDER' => [
            'NAME' => 'Some custom string here...',
        ]
    ]
],
like image 168
Sandyandi N. dela Cruz Avatar answered Sep 27 '22 19:09

Sandyandi N. dela Cruz