Reputation: 9047
I'm trying to loop through the error message bag ($validator->errors()->getMessages()) array and push its objects to another array ($error)
$error = array();
$validator = Validator::make($request->all(),[
'username' => 'required|unique:User',
'password' => 'required',
'fullname' => 'required|unique:Profile',
'email' => 'required|email|unique:Profile'
]);
if($validator->fails()) :
foreach($validator->errors()->getMessages() as $m => $key):
array_push($error,$key);
endforeach;
endif;
and then loop unto the new array ($error) and create a string base on the new array objects ($str)
$str = '<ul class="c_red padding_zero margin_zero menu">';
foreach($error as $e ){ // this is the line 113
$str.= '<li>'.$e.'</li>';
}
$str.='</ul>';
return array('success' => false, 'message' => $error );
but it returns me an error
Array to string conversion line 113
here's the response when not loop to $error array to create $str string
{"success":false,"message":[["The username field is required."],["The password > field is required."],["The fullname field is required."],["The email field is > required."]]}
Upvotes: 4
Views: 2183
Reputation: 11677
each key in the errors may contains more then one error so it will be a string...
so you could do this:
if($validator->fails()) :
foreach($validator->errors()->getMessages() as $validationErrors):
if (is_array($validationErrors)) {
foreach($validationErrors as $validationError):
$error[] = $validationError;
endforeach;
} else {
$error[] = $validationErrors;
}
endforeach;
endif;
here you will get an array of all error messages...
Upvotes: 5