Answer the question
In order to leave comments, you need to log in
How to validate array values?
In my Request class, the rules look like this in short:
public function rules()
{
$rules = [
'detail_id' => 'required|array',
];
$detailId = $this->input('detail_id');
foreach ($detailId as $index => $value){
$rules["detail_id[".$index."]"] = 'json';
}
return $rules;
}
array:21 [▼
"detail_id" => "required|array"
"detail_id[0]" => "json"
"detail_id[1]" => "json"
"detail_id[2]" => "json"
"detail_id[3]" => "json"
"detail_id[4]" => "json"
"detail_id[5]" => "json"
]
dd($factory->make(
$this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
));
Validator {#266 ▼
#translator: Translator {#100 ▶}
#presenceVerifier: DatabasePresenceVerifier {#101 ▶}
#container: Application {#3 ▶}
#failedRules: []
#messages: null
#data: array:5 [▼
"_method" => "PATCH"
"_token" => "yKG4oqNtBJmHIDGAYEwepoQZ13NVXsRPEWGV4oPJ"
"detail_id" => array:6 [▼
0 => "{"id":6,"base_detail_name":"\u30d7\u30ed\u30aa\u30da\u30ec\u30fc\u30bf\u30fc\uff08\u8ffd\u52a0\u5206\uff09","base_image1":"images\\base_detail\\no-image.jpg","base_image2":"images\\base_detail\\no-image.jpg","base_price":54665,"base_unit":"\u540d","deleted_at":null}"
1 => "{id":...."deleted_at":null}"
2 => "{id":...."deleted_at":null}"
3 => "{id":...."deleted_at":null}"
4 => "{id":...."deleted_at":null}"
5 => "{id":...."deleted_at":null}"
]
]
#files: []
#rules: array:21 [▼
"detail_id" => array:2 [▼
0 => "required"
1 => "array"
]
"detail_id[0]" => array:1 [▼
0 => "json"
]
"detail_id[1]" => array:1 [▶]
"detail_id[2]" => array:1 [▶]
"detail_id[3]" => array:1 [▶]
"detail_id[4]" => array:1 [▶]
"detail_id[5]" => array:1 [▶]
]
#after: []
#customMessages: []
#fallbackMessages: []
#customAttributes: []
#customValues: []
#extensions: array:6 [▶]
#replacers: []
#sizeRules: array:4 [▶]
#numericRules: array:2 [▶]
#implicitRules: array:8 [▶]
}
Answer the question
In order to leave comments, you need to log in
$rules = [
'detail_id' => 'required|array',
'detail_id.*' => 'json',
];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question