如何在验证消息 Laravel 5.2 中获取数组索引

问题描述 投票:0回答:2

我将这些数组作为参数放入

Laravel Validator
中:

['item.*' => 'string'] // rules

['item.*.string' => 'Item number (index) is not string'] // messages

我想在验证消息中包含

index number
。上面的代码只是为了演示,并不能工作。如何做到这一点?

laravel-5 laravel-validation
2个回答
8
投票

尝试这个或使用这个

    'name' : [ { 'value' : 'raju' } , { 'value' : 'rani'} ]       

并通过

进行验证
    'name.*' or 'name.*.value' => 'required|string|min:5'       

消息将是

    'name.*.required' => 'The :attribute is required'       
    'name.*.value.required' => 'The :attribute is required'      

我想这会对你有帮助..

试试这个,

public function messages()
{
    $messages = [];
    foreach ($this->request->get('name') as $key => $value){
        $messages['name.'. $key .'.required'] = 'The item '. $key .'  is not string';
    }
    return $messages;
}

0
投票

在新版本的 Laravel 中你可以尝试:

'item.*.string' => 'Item number :position is not string'

:position 将替换为人类可读的数字,例如 0 索引转换为 1。

© www.soinside.com 2019 - 2024. All rights reserved.