9

In a package, I’m trying to have custom validation error messages in a file separated from the default one of Laravel. I’ve duplicated the file, changed the values (did not touch the keys), loaded it correctly in the service provider and everything worked fine. Base validations such as required, numeric, email, etc displayed the text in my file. Now, I tried to use the size rule, but since the error messages are in an array, it gives me this Exception when using $errors->get( 'content.reasons' ):

Array to string conversion in MessageBag.php (line 246)

Here the important parts of the code

Controller

$validator = Validator::make( $request->all(), $rules, trans( 'admin::validation' ) );
if ( $validator->fails() ){
    return redirect()
        ->back()
        ->withInput()
        ->withErrors( $validator );
}

$request->all()

[
    // ...
    "content" => [
        "reasons" => [
            [...],
            [...]
        ]
    ],
    // ...
]

$rules

[
    // ...
    "content.reasons" => "required|size:3"
    // ...
]

trans( 'admin::validation' )

// Exact structure of Laravel validation.php
[
    // ...
    'size'                 => [
        'numeric' => 'The field must be :size.',
        'file'    => 'The field must be :size kilobytes.',
        'string'  => 'The field must be :size characters.',
        'array'   => 'The field must contain :size items.',
    ],
    // ...
]

After the redirect

$errors = session()->get('errors') ?: new ViewErrorBag;
$field[ 'errors' ] = $errors->get( $dot_name ); // Exception thrown when $dot_name
                                                // equals `content.reasons`.

$errors

Illuminate\Support\ViewErrorBag {
    #bags: array:1 [
        "default" => Illuminate\Support\MessageBag {
            #messages: array:4 [
                "content.why_title" => array:1 [
                    0 => "The field is required."
                ]
                "content.reasons" => array:1 [
                    0 => array:4 [
                        "numeric" => "The field must be 3."
                        "file" => "The field must be 3 kilobytes."
                        "string" => "The field must be 3 characters."
                        "array" => "The field must contain 3 items."
                    ]
                ]
                "content.reasons.0.icon" => array:1 [
                    0 => "The field is required."
                ]
                "content.reasons.0.text" => array:1 [
                    0 => "The field is required."
                ]
            ]
            #format: ":message"
        }
    ]
}

I've tried passing content.reasons.array but it returns an empty array.

I've also tried to pass no messages (use Laravel's defaults) and it works, but I really need custom messages...

TL;DR: How can we pass custom messages with the same schema as Laravel?

4
  • It means you're trying to echo an array Commented Aug 25, 2017 at 3:25
  • @vietnguyen09 I know what it means, the question is how can I pass a custom error messages array with the same structure as Laravel and still works with rules that use an array as the message (like size). Hope it clarifies the question. Commented Aug 25, 2017 at 12:59
  • I faced similar error last few days and here is my way to make it works by 'size' => 'The field must be :size.' , but with this way, I have to say goodbye to other types. Commented Aug 25, 2017 at 13:26
  • 1
    @vietnguyen09 Yes, that's what I've done as well. It seems more like a bug than a suitable question on SO. I've posted it as an issue on the Laravel GitHub page (github.com/laravel/framework/issues/20754). Commented Aug 25, 2017 at 13:30

1 Answer 1

1

I don't know what are you trying to do, but you can pass custom messages for each validation field/rule, for example, in your controller:

    //Error messages
    $messages = [
        "name.required" => "Name is required",
        "name.string" => "Name must be a string",
        "username.required" => "Username is required",
        "username.min" => "Username must be at least 6 characters",
        "email.required" => "Email is required",
        "email.email" => "Email is not valid"
    ];

    // validate the form data
    $validator = Validator::make($request->all(), [
            'name' => 'required|string',
            'username' => 'required|min:6',
            'email' => 'required|email'
        ], $messages);

    if ($validator->fails()) {
        return back()->withErrors($validator)->withInput();
    } else {
        //Some code here
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I am aware of that, but I need it to be much more dynamic than explicitly passing the error messages. Thanks anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.